问题
I am trying to use openMP, and I have this one function that can never be run two times at the same time. In another world, this would not be a problem:
int foo(void){
mutex->lock();
....
mutex->release();
}
How can I achieve the same thing in OpenMP?
回答1:
Use:
#pragma omp critical (CriticalSection1)
{
// your stuff here
}
EDIT
I hope this is clearer:
int foo(void){
//mutex->lock();
#pragma omp critical (CriticalSection_foo)
{
....
}
//mutex->release();
}
EDIT 2
I extended the example to include named critical section, as advised by the commenters. The round brackets are necessary, see the Intel OMP documentation with an example.
来源:https://stackoverflow.com/questions/19682767/critical-an-entire-function