Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange concurrency issue with STL / OpenMP in 64 bit builds
    text
    copied!<p>I have an odd issue when I build one of our projects in a 64 bit debug config. It appears to produce some strange behaviour which looks a little like a single iterator is getting incremented multiple times. I've narrowed it down to the following test code:</p> <pre><code>#include &lt;omp.h&gt; #define _HAS_ITERATOR_DEBUGGING 0 #include &lt;vector&gt; #include &lt;set&gt; int main(int argc, const char* argv[]) { std::vector&lt;int&gt; v; for(int j = 0; j &lt; 20; ++j) { v.push_back(j); } #pragma omp parallel for for(int i = 0; i &lt; 100000; ++i) { std::set&lt;int&gt; s; std::vector&lt;int&gt;::const_iterator begin = v.begin(), end = v.end(); s.insert(begin, end); // after this line s should contain the numbers 0-19 if(s.size() != 20) { printf("fail\n"); exit(3); } } return 0; } </code></pre> <p>However, the size test frequently fails which implies that somehow it is not inserting the entire contents of the vector - and a lot of poking at it makes it look rather like the vector iterators are being incremented more than one step at a time. It's quite hard to tell though since it doesn't tend to happen if one breaks in with the debugger.</p> <p>The obvious conclusion to draw would be that it's not threadsafe, but my understanding is that it should be because the only variable modified is <code>s</code> which has local scope. </p> <p>There are quite a few things which will fix the immediate problem:</p> <ul> <li>remove the parallel for</li> <li>throw a critical section around the <code>insert()</code> call</li> <li><code>#define HAS_ITERATOR_DEBUGGING 1</code></li> <li>replace the single insert() call with a manual loop and insert each item individually (this is basically what that function does internally, but the problem definitely goes away when I do it myself)</li> <li>build a 32bit version of the same code</li> <li>build the release version of the same code</li> </ul> <p>This is being compiled under MSVC++ 2008 SP1, with the compiler-supplied STL implementation.</p> <p>Can anyone shed any light on what's going on here? Thanks in advance for any hints - I'm pretty stumped :)</p> <p>Edit: In case it is not clear, I am not looking for a "quick fix" to make this code work; as noted above I know of quite a few of those. What I want to understand is why this problem is occurring in the first place.</p> <p>Edit 2: Code works correctly when compiled with gcc.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload