“critical” an entire function

白昼怎懂夜的黑 提交于 2020-01-05 07:24:04

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!