基本上我需要主线程根据一些全局变量的值继续执行一些操作,这些全局变量可以由辅助线程(以某些选定的时间间隔)编辑。就像是:
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();
}
}