tbb "task" local storage

Viewed 131

Assume I have the following code

tbb::enumerable_thread_specific<std::set<int>> tls;
tbb::parallel_for(0, n, [&] (int i) {
  auto &traversedVisited = tls.local();
  // do a bfs using traversedVisited
});

Assume no other parallel_for are happening in the bfs parts (avoiding the problem mentioned in the task_isolation documentation). I know tls variable will provide thread-local storage, so there are no contentions between parallel_for tasks that are spawn on the same thread. However, if I understand correctly, those tasks (that are spawned on the same thread) share the same thread-local storage. Suppose tasks A and B are spawned on thread 1. Is it possible A is executed halfway and paused and B is turned in and starts to execute? If that is possible, then "traversedVisited" may not be valid for both A and B. Or are tbb tasks always atomic? If not, what's the solution for the situation mentioned at the beginning?

1 Answers

Both tasks A & B are independent of each other based on their dependencies. They will execute parallelly in the same task arena with individual local storage(TLS). And by default, TBB tasks are not atomic, we need to explicitly make them atomic based on the use-case.

For more information refer to the concept "The TBB Thread Pool (the Market) and Task Arenas" from the below link: https://link.springer.com/content/pdf/10.1007%2F978-1-4842-4398-5.pdf

Related