如何在openMP使用锁? [英] How to use lock in openMP?

查看:880
本文介绍了如何在openMP使用锁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个C ++代码运行在2个不同的核心。
两者都wirte到同一个文件。
如何使用openMP并确保没有崩溃?

I have two piece of C++ code running on 2 different cores. Both of them wirte to the same file. How to use openMP and make sure there is no crash?

推荐答案

您想要 OMP_SET_LOCK / OMP_UNSET_LOCK 功能: https://computing.llnl.gov/tutorials/openMP/#OMP_SET_LOCK。基本上:

You want the OMP_SET_LOCK/OMP_UNSET_LOCK functions: https://computing.llnl.gov/tutorials/openMP/#OMP_SET_LOCK. Basically:

omp_lock_t writelock;

omp_init_lock(&writelock);

#pragma omp parallel for
for ( i = 0; i < x; i++ )
{
    // some stuff
   omp_set_lock(&writelock);
    // one thread at a time stuff
    omp_unset_lock(&writelock);
    // some stuff
}

omp_destroy_lock(&writelock);

大多数锁定例程,例如pthreads semaphores和sysv信号量适用于那种逻辑,呼叫不同。

Most locking routines such as pthreads semaphores and sysv semaphores work on that sort of logic, although the specific API calls are different.

这篇关于如何在openMP使用锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆