Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
2464 Discussions

How to initialize local variable for each thread (parallel_for)? - Seems like a bug of parallel_for (TBB 4.0)

zhaonaiy
Beginner
1,713 Views
Greetings,
I am using TBB 4.0 on Windows (VS 2010), and trying to to parallel a loop using parallel_for. However, I encountered a local variable initialization problem.
class getIteratorArrayBody
{
//some other private variable declarition
int *rOffsetArray;
public:
getIteratorArrayBody(..., int * r):rOffsetArray{}
void operator()( const tbb::blocked_range& range ) const
{
size_t iterator; // thread local variable
for (size_t j=range.begin(); j!=range.end(); j++) {// code to do some calculation
iterator= ....
rOffsetArray = iterator;
}
...
}
............
int * IndexingEngine:: getIteratorArrayInParallel (..., tbb::affinity_partitioner &affinity)
{
int *rOffsetArray = new unsigned int [100000];
tbb::parallel_for( tbb::blocked_range( 0, vLBASpaceLength ), // Index space for loop getIteratorArrayBody (...,rOffsetArray), // Body of loop
affinity ); // Affinity hint
returnrOffsetArray;
}
For the above code, if I specify number of threads to 1, then no problem to execute.
However if I specify number of threads to 64, then it reports "The variable 'iterator' is being used without being initialized"
From this, it seems this problem is caused by conflict between threads. I did try some other methods to initialize it, however all failed.
Any suggestion to resovle this initizalition problem is very appreciated!!
Nai Yan.
0 Kudos
21 Replies
Alexey-Kukanov
Employee
141 Views
I certainly agree, and most likely this causes all observed problematic sympthoms.Even if file processingroutines are "thread-safe" internally so that only one thread can work with the givenfile at any moment, the interleaving described by Raf remains quite possible.

Protect reading a page from the file with a lock, and check whether it solves your problems.
0 Kudos
Reply