OpenMP和线程间通讯机制

OpenMP规范中没有提供类似Pthread的条件变量这种线程间通讯的机制,我们只能用flush指令来模拟。下面是一个采用OpenMP的flush指令实现的生产者消费者的例子。

int main()
{
double *A, sum, runtime; int numthreads, flag = 0;
A = (double *)malloc(N*sizeof(double));
#pragma omp parallel sections
{
#pragma omp section
{
fill_rand(N, A);
#pragma omp flush
flag = 1;
#pragma omp flush (flag)
}
#pragma omp section
{
#pragma omp flush (flag)
while (flag != 1){
#pragma omp flush (flag)
}
#pragma omp flush
sum = Sum_array(N, A);
}
}
}