Search code examples
c++tbb

Thread Local Storage: How to combine values?


I have a parallel_for loop:

tbb::parallel_for(tbb::blocked_range<int>(0, numVerts, 1 /* Grain Size */),
                  [&](tbb::blocked_range<int> r) {
                    thread_local vector<int> V;
                    for (int i = r.begin(); i < r.end(); ++i) {
                    ... (some work changing values in V)
                    }
                  }

and I want to "combine" these values once the parallel work is done, but how? Is there a way to do that?


Solution

  • By definition, thread-local storage is local to each thread. Like any other local variable, the only way to access it across threads is to pass around pointers.

    But you don't need to do that in this case (you don't need the thread_local, either). You know how many vectors you need, so you should just allocate an array of vectors before calling parallel_for(), letting the lambda iterate the blocked_range populating each vector by index as needed. When parallel_for is done, you can then iterate the array to collect the results.

    vector<vector<int>> V(numVerts);
    
    tbb::parallel_for(tbb::blocked_range<int>(0, numVerts, 1 /* Grain Size */),
        [&](tbb::blocked_range<int> r) {
            for (int i = r.begin(); i < r.end(); ++i) {
                // some work changing values in V[i] ...
            }
        }
    );
    
    // use V as needed...