基本上我需要主线程继续根据一些全局变量的值进行一些操作,这些全局变量可以由辅助线程编辑(在某些选定的时间间隔)。是这样的:OpenMP将一个函数的执行分配给一个线程?
vector<int> mySharedVar;
void secondaryThreadFunction() {
Do some operations
And update mySharedVar if necessarily
}
int main() {
int count = 0;
while(true) {
count++;
if (count%100) { //> Each 100 iterations run a parallel thraed
//> RUN secondaryThreadFunction in a separateThread
}
this is the main thread that based its operation on mySharedVar
}
}
这是OpenMP的命令与secondaryThreadFunction();
运行单个并行线程?
是否有比这任何其他更好的办法:
#pragma omp parallel num_threads(2)
{
int i = omp_get_thread_num();
if (i == 0){
mainThread();
}
if (i == 1 || omp_get_num_threads() != 2){
secondaryThreadFunction();
}
}
我想是OpenM P部分是少一点hacky。 – Mysticial 2012-07-18 09:47:35
@Mysticial:嗨,大声笑,我从你的答案采取该示例:D http://stackoverflow.com/questions/7876156/openmp-run-two-functions-in-parallel-each-by-half-of-thread-无论如何,你可以给我看一个样本吗? :D – dynamic 2012-07-18 09:49:53
哈哈,这个问题似乎有一个很好的例子:http://stackoverflow.com/questions/2770911/how-does-the-sections-directive-in-openmp-distribute-work – Mysticial 2012-07-18 09:52:10