我在Windows 7机器上使用Visual Studio 2010和OpenMP。我已经写了下面的代码snipplet:C++ OpenMP退出并行循环内部对于
#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
我的问题是:
如果我使用b_continue
变量,第一while循环到达中止条件导致程序挂起(这是一个竞争条件,我没有看到?),结果程序不执行其余的代码。
那么,我该如何得到这个工作?
我试过breaking out of structured block in openmp中建议的解决方案,但这并没有改变这种情况。
需要selfdestuction while循环来触发从一个硬件下载文件。完成后,程序应该对该文件做一些工作。在开始平行化代码之前,它使用while循环中的break语句运行良好。
感谢您的任何意见,这给我一个提示来解决这个问题。
更新:我现在改变了代码
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;
}
}
全局变量_i_NoDownloads
计数的下载文件的数量,如果达到2个的消息循环不再需要。但是,即使发生这种变化,程序的行为也不会改变,尽管事实是,while循环现在不一定是50次迭代。我认为问题是同时降级消息循环。
更新2:在阅读了很多关于Windows消息的概念之后,我发现了一个可行的解决方案。并行部分没有完成的原因是GetMessage的行为。
再次,非常感谢任何对此问题作出贡献的人。
干杯 TL
在不同的OpenMP线程中运行相同的消息循环?我无法想象这会如何表现。消息循环只属于一个线程。你需要完全重新设计你的代码。 – 2013-05-08 08:50:28
从一些显示i_loopcounter和omp_get_thread_num()的cout命令中,似乎有两个消息循环,因为我同时获得了两个线程号(0和1) – TLkuebler 2013-05-08 08:59:38