【发布时间】:2019-11-02 00:37:04
【问题描述】:
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) 的整数之和。这个属性在其他线程库中使用,比如openmp,pthread,会不会被持有?例如,
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);
上面的代码会不会导致不可预知的结果?
【问题讨论】:
标签: c++ tbb thread-local-storage