Community
cancel
Showing results for 
Search instead for 
Did you mean: 
adam_h_2
Beginner
49 Views

Strange Result Using Parallel_For on Simple Example using VS2017

Using VS2017 v15.9.7 on Windows 10 and latest Intel libraries, the following code produces a very strange output. 

    int nSamples = 10000 ;
    std::vector<int>idx(nSamples);
	std::iota(idx.begin(), idx.end(), 0);

		tbb::parallel_for(tbb::blocked_range<size_t>(0, nSamples),
			[&](const tbb::blocked_range<size_t>& r) {
			for (int i = r.begin(); i != r.end(); ++i)
				std::cout << idx.at(i) << std::endl ;
		});

 

I would obviously expect this to display all the ints from 0 to 9999 on the command line (not in sequential order, as intel TBB will branch them). However, instead I get some lines of output that are blank and some where it appears it has tried to access outside the range and displays clearly from somewhere else in memory e.g.

01020

30
31
2150

60

32
11
40
41
4222
 

Am I missing something about how one uses TBB? Any help much appreciated.

0 Kudos
1 Reply
Alexei_K_Intel
Employee
49 Views

The output with std::cout can be interleaved between multiple threads. E.g. idx.at() from one thread and idx.at() from another thread can be printed on the same line but then two std::endl will printed in the row (giving an empty line in the output). Try to use std::printf("%d\n", idx.at(i)) instead of std:cout.

Reply