Search code examples
c++tbb

TBB: Possible to get Thread IDs?


I have a very simple parallel_for loop

    tbb::parallel_for(tbb::blocked_range<int>(0, values.size()),
    [&](tbb::blocked_range<int> r)
    {
        for (int i = r.begin(); i < r.end(); ++i)
        {
            values[i] = std::sin(i * 0.001);
        }
    });

Where 'values' is a vector of doubles. What I'd like to know is which threads work on which range in the loop. Is it possible to get a thread ID of some kind from TBB?


Solution

  • Also, if you want to know a relative number of worker thread in current task_arena, which goes from 0 to the arena concurrency level, use this:

    int worker_index = tbb::task_arena::current_thread_index();
    

    The range of index values can be contiguous if all the threads will get to work at the same time.