访问/设置 Rcpp::List 线程中的元素是否安全? [英] Is accessing/setting elements in a Rcpp::List thread safe?

查看:60
本文介绍了访问/设置 Rcpp::List 线程中的元素是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Rcpp 中对列表列表执行有效的并行 lapply,有效地从列表中获取/删除元素并使用它们进行计算.基本上做 Rcpp parallel 所做的,但使用列表而不是数字向量.如果通过数字索引完成,Rcpp 是否正在获取/设置列表元素线程安全?

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;
 };

```

推荐答案

你问

如果通过数字索引完成,在 Rcpp 中获取/设置列表元素线程安全吗?

In Rcpp is getting/setting a list element thread safe if done by numeric index?

答案(通常)是坚定的不",因为(给予或接受)没有关于 R 和 R 数据结构.

and the answer is (generally) a firm "No" because (give or take) nothing concerning R and R data structures is.

查看现有的关于使用 R 的 OpenMP 的文章,包括 编写 R 扩展——在 1.2.1.1 节中明确指出

See existing write ups on use of OpenMP with R including Writing R Extensions -- which states flatly in Section 1.2.1.1 that

从线程代码调用任何 R API仅供专家使用":他们需要阅读源代码以确定它是否是线程安全的.特别是,不能从线程代码中调用使用堆栈检查机制的代码.

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.

另请参阅我们的 OpenMP 上的 Rcpp Gallery 以及 RcppParallel 及其更多示例.

Also see our Rcpp Gallery on OpenMP as well and the fine documentation for RcppParallel and its examples for more.

这篇关于访问/设置 Rcpp::List 线程中的元素是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆