- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page