| 1 |
--- |
| 2 |
title: "Using later from C++" |
| 3 |
author: "Joe Cheng" |
| 4 |
date: "`r Sys.Date()`" |
| 5 |
output: rmarkdown::html_vignette |
| 6 |
vignette: > |
| 7 |
%\VignetteIndexEntry{Using later from C++} |
| 8 |
%\VignetteEngine{knitr::rmarkdown} |
| 9 |
%\VignetteEncoding{UTF-8} |
| 10 |
--- |
| 11 |
|
| 12 |
# Using later from C++ |
| 13 |
|
| 14 |
You can call `later::later` from C++ code in your own packages, to cause your own C-style functions to be called back. This is safe to call from either the main R thread or a different thread; in both cases, your callback will be invoked from the main R thread. |
| 15 |
|
| 16 |
To use the C++ interface, you'll need to: |
| 17 |
|
| 18 |
* Add `later` to your `DESCRIPTION` file, under both `LinkingTo` and `Imports` |
| 19 |
* Make sure that your `NAMESPACE` file has an `import(later)` entry. If your package uses roxygen2, you can do this by adding the following lines to any file under `R/`: |
| 20 |
``` |
| 21 |
#' @import later |
| 22 |
NULL |
| 23 |
``` |
| 24 |
* Add `#include <later_api.h>` to the top of each C++ file that uses the below APIs. |
| 25 |
|
| 26 |
## Executing a C function later |
| 27 |
|
| 28 |
The `later::later` function is accessible from `later_api.h` and its prototype looks like this: |
| 29 |
|
| 30 |
```cpp |
| 31 |
void later(void (*func)(void*), void* data, double secs) |
| 32 |
``` |
| 33 |
|
| 34 |
The first argument is a pointer to a function that takes one `void*` argument and returns void. The second argument is a `void*` that will be passed to the function when it's called back. And the third argument is the number of seconds to wait (at a minimum) before invoking. In all cases, the function will be invoked on the R thread, when no user R code is executing. |
| 35 |
|
| 36 |
## Background tasks |
| 37 |
|
| 38 |
This package also offers a higher-level C++ helper class called `later::BackgroundTask`, to make it easier to execute tasks on a background thread. It takes care of launching the background thread for you, and returning control back to the R thread at a later point; you're responsible for providing the actual code that executes on the background thread, as well as code that executes on the R thread before and after the background task completes. |
| 39 |
|
| 40 |
Its public/protected interface looks like this: |
| 41 |
|
| 42 |
```cpp |
| 43 |
class BackgroundTask { |
| 44 |
|
| 45 |
public: |
| 46 |
BackgroundTask(); |
| 47 |
virtual ~BackgroundTask(); |
| 48 |
|
| 49 |
// Start executing the task |
| 50 |
void begin(); |
| 51 |
|
| 52 |
protected: |
| 53 |
// The task to be executed on the background thread. |
| 54 |
// Neither the R runtime nor any R data structures may be |
| 55 |
// touched from the background thread; any values that need |
| 56 |
// to be passed into or out of the Execute method must be |
| 57 |
// included as fields on the Task subclass object. |
| 58 |
virtual void execute() = 0; |
| 59 |
|
| 60 |
// A short task that runs on the main R thread after the |
| 61 |
// background task has completed. It's safe to access the |
| 62 |
// R runtime and R data structures from here. |
| 63 |
virtual void complete() = 0; |
| 64 |
} |
| 65 |
``` |
| 66 |
|
| 67 |
Create your own subclass, implementing a custom constructor plus the `execute` and `complete` methods. |
| 68 |
|
| 69 |
It's critical that the code in your `execute` method not mutate any R data structures, call any R code, or cause any R allocations, as it will execute in a background thread where such operations are unsafe. You can, however, perform such operations in the constructor (assuming you perform construction only from the main R thread) and `complete` method. Pass values between the constructor and methods using fields. |
| 70 |
|
| 71 |
```rcpp |
| 72 |
#include <Rcpp.h> |
| 73 |
#include <later_api.h> |
| 74 |
|
| 75 |
class MyTask : public later::BackgroundTask { |
| 76 |
public: |
| 77 |
MyTask(Rcpp::NumericVector vec) : |
| 78 |
inputVals(Rcpp::as<std::vector<double> >(vec)) { |
| 79 |
} |
| 80 |
|
| 81 |
protected: |
| 82 |
void execute() { |
| 83 |
double sum = 0; |
| 84 |
for (std::vector<double>::const_iterator it = inputVals.begin(); |
| 85 |
it != inputVals.end(); |
| 86 |
it++) { |
| 87 |
|
| 88 |
sum += *it; |
| 89 |
} |
| 90 |
result = sum / inputVals.size(); |
| 91 |
} |
| 92 |
|
| 93 |
void complete() { |
| 94 |
Rprintf("Result is %f\n", result); |
| 95 |
} |
| 96 |
|
| 97 |
private: |
| 98 |
std::vector<double> inputVals; |
| 99 |
double result; |
| 100 |
}; |
| 101 |
``` |
| 102 |
|
| 103 |
To run the task, `new` up your subclass and call `begin()`, e.g. `(new MyTask(vec))->begin()`. There's no need to keep track of the pointer; the task object will delete itself when the task is complete. |
| 104 |
|
| 105 |
```r |
| 106 |
// [[Rcpp::export]] |
| 107 |
void asyncMean(Rcpp::NumericVector data) { |
| 108 |
(new MyTask(data))->begin(); |
| 109 |
} |
| 110 |
``` |
| 111 |
|
| 112 |
It's not very useful to execute tasks on background threads if you can't get access to the results back in R. We'll soon be introducing a complementary R package that provides a suitable "promise" or "future" abstraction. |