当tbb::enumerable_thread_specific
在tbb并行块中使用时,它提供线程本地存储。例如,
tbb::enumerable_thread_specific<int> tls(0);
tbb::parallel_for(0, n, [&] (int i) {
tls.local() += i;
});
std::accumulate(tls.begin(), tls.end(), 0);
在lambda函数中,tls.local()不会被其他线程同时使用。当我们在tls
中累加值时,它应该给出从0到(n-1)的整数和。在其他线程库中使用此属性时,是否会保留此属性?例如,
tbb::enumerable_thread_specific<int> tls(0);
#pragma omp parallel for
for (int i = 0; i < n; i++) {
tls.local() += i;
}
std::accumulate(tls.begin(), tls.end(), 0);
上面的代码会导致不可预知的结果吗?
发布于 2019-11-05 18:44:22
tbb::enumerable_thread_specific
也应该与非TBB线程一起工作。请查看其本地存储是如何通过relevant source lines进行管理的。我们可以看到,这并不是TBB的特性,而是使用了操作系统通用的API。
https://stackoverflow.com/questions/58667316
复制相似问题