Is accessing/setting elements in a Rcpp::List thread safe?

≯℡__Kan透↙ 提交于 2019-12-24 06:33:40

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!