title: C++ OpenMP exit while loop inside a parallel for
body: <p>I use Visual Studio 2010 and OpenMP on a Windows 7 machine. I've written the following code snipplet:</p>
  
  <pre><code>#pragma omp parallel for
  for(int jj=0;jj&lt;2;jj++){
  MSG msg;
  // do something in parallel for jj
  int i_loopcounter = 0;
  bool b_continue = true;
  while(GetMessage(&amp;msg, NULL, 0, 0)&amp;&amp;b_continue){
      TranslateMessage(&amp;msg);
      DispatchMessage(&amp;msg);
      i_loopcounter++;    
      if(i_loopcounter==50){
          b_continue = false;
          //abort(); &lt;- ugly, since a window appears  
          //exit(0); &lt;- rest of the code is not executed
          //break;  &lt;- not allowed
          //goto end; &lt;- not allowed
      }
  }
  end:;
  // do some other stuff in parallel for jj
  } //end of parallel for
  </code></pre>
  
  <p>My problem is:</p>
  
  <p>If I use the <code>b_continue</code> variable, the first while loop which reaches the abort condition causes the program to hang-up (Is this a race condition, which I don't see?), as a result the program does not execute the rest of the code.</p>
  
  <p>So how can I get this working?</p>
  
  <p>I've tried the solution suggested in <a href="https://stackoverflow.com/questions/15858499/breaking-out-of-structured-block-in-openmp">breaking out of structured block in openmp</a> but this does not change the situation.</p>
  
  <p>The selfdestucting while loop is needed to trigger a download of a file from a piece of hardware. After it is finished the program should do some work on that file. Before I started to parallelise the code it worked well using the break statement in the while loop.</p>
  
  <p>Thanks for any comments, which give me a hint to solve this issue.</p>
  
  <p><strong>Update:</strong> I now changed the code to </p>
  
  <pre><code>int i_loopcounter = 0;
      while(_i_NoDownloads!=2){
          GetMessage(&amp;msg, NULL, 0, 0);
          TranslateMessage(&amp;msg);
          DispatchMessage(&amp;msg);
          i_loopcounter++;    
          if(_b_control){
              cout&lt;&lt;i_loopcounter &lt;&lt;" of Thread "&lt;&lt;omp_get_thread_num()&lt;&lt;endl;
          }       
      }
  </code></pre>
  
  <p>The global variable <code>_i_NoDownloads</code> counts the number of downloaded files, if it reaches 2 the message loops are no longer needed. However, even with this changes the behavior of the program does not change, despite the fact that, the while loop now does not necessarily 50 iterations. I think that the problem is to desturct the message loops simultaneously.</p>
  
  <p><strong>Update 2:</strong> After Reading a lot about the concept of Windows Messages, I found a solution which works. The reason why the parallel section did not finish is the behavior of GetMessage.</p>
  
  <p>Again, thanks a lot, to anybody who contributed to this issue.</p>
  
  <p>Cheers
  TL</p>