简体   繁体   中英

C++ OpenMP exit while loop inside a parallel for

I use Visual Studio 2010 and OpenMP on a Windows 7 machine. I've written the following code snipplet:

#pragma omp parallel for
for(int jj=0;jj<2;jj++){
MSG msg;
// do something in parallel for jj
int i_loopcounter = 0;
bool b_continue = true;
while(GetMessage(&msg, NULL, 0, 0)&&b_continue){
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    i_loopcounter++;    
    if(i_loopcounter==50){
        b_continue = false;
        //abort(); <- ugly, since a window appears  
        //exit(0); <- rest of the code is not executed
        //break;  <- not allowed
        //goto end; <- not allowed
    }
}
end:;
// do some other stuff in parallel for jj
} //end of parallel for

My problem is:

If I use the b_continue 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.

So how can I get this working?

I've tried the solution suggested in breaking out of structured block in openmp but this does not change the situation.

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.

Thanks for any comments, which give me a hint to solve this issue.

Update: I now changed the code to

int i_loopcounter = 0;
    while(_i_NoDownloads!=2){
        GetMessage(&msg, NULL, 0, 0);
        TranslateMessage(&msg);
        DispatchMessage(&msg);
        i_loopcounter++;    
        if(_b_control){
            cout<<i_loopcounter <<" of Thread "<<omp_get_thread_num()<<endl;
        }       
    }

The global variable _i_NoDownloads 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.

Update 2: 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.

Again, thanks a lot, to anybody who contributed to this issue.

Cheers TL

As you seem to use a small and hard-coded number of iterations:

#pragma omp parallel for
for(int jj = 0; jj < 2; jj++){
 /* ... */
} //end of parallel for

you may possibly want to explore solutions based on the sections directive:

#pragma omp parallel
{
#pragma omp sections
  {
#pragma omp section
    {
      /* jj = 0 */
    }         
#pragma omp section
    {
      /* jj = 1 */
    }             
  } // End sections
#pragma omp single
  { 
    /* While loop */
  }
  /* ... */      
} // End parallel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM