File: bgtest.cpp

package info (click to toggle)
r-cran-later 1.1.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 412 kB
  • sloc: cpp: 1,473; ansic: 1,096; sh: 29; makefile: 2
file content (33 lines) | stat: -rw-r--r-- 892 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <later_api.h>
#include <unistd.h>

// Used in tests/testthat/test-run_now.R

class TestTask : public later::BackgroundTask {
  int _timeoutSecs;

public:
  TestTask(int timeoutSecs) : _timeoutSecs(timeoutSecs) {}

protected:
  // The task to be executed on the background thread.
  // Neither the R runtime nor any R data structures may be
  // touched from the background thread; any values that need
  // to be passed into or out of the Execute method must be
  // included as fields on the Task subclass object.
  void execute() {
    sleep(_timeoutSecs);
  }

  // A short task that runs on the main R thread after the
  // background task has completed. It's safe to access the
  // R runtime and R data structures from here.
  void complete() {}
};


// [[Rcpp::depends(later)]]
// [[Rcpp::export]]
void launchBgTask(int secsToSleep) {
  (new TestTask(secsToSleep))->begin();
}