问题
I want to perform what is effectively a parallel lapply inside Rcpp on a list of lists, effectively taking/removing elements from a list and computing with them. Basically doing what Rcpp parallel does but with lists instead of numeric vectors. In Rcpp is getting/setting a list element thread safe if done by numeric index?
Pseudo code for the type of thing I want to do below:
List original = // created somewhere else,
List results = List(original.size());
// Is this function thread safe?
auto func = [original&, results&](int i) {
List data = original[i];
// Calculate a new List, stuff
results[i] = stuff;
};
```
回答1:
You ask
In Rcpp is getting/setting a list element thread safe if done by numeric index?
and the answer is (generally) a firm "No" because (give or take) nothing concerning R and R data structures is.
See existing write ups on use of OpenMP with R including Writing R Extensions -- which states flatly in Section 1.2.1.1 that
Calling any of the R API from threaded code is ‘for experts only’: they will need to read the source code to determine if it is thread-safe. In particular, code which makes use of the stack-checking mechanism must not be called from threaded code.
Also see our Rcpp Gallery on OpenMP as well and the fine documentation for RcppParallel and its examples for more.
来源:https://stackoverflow.com/questions/38669655/is-accessing-setting-elements-in-a-rcpplist-thread-safe