OpenMP 临界区与锁 [英] OpenMP critical section vs locks

查看:69
本文介绍了OpenMP 临界区与锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OpenMP 锁和临界区有什么区别?它们只是彼此的替代品吗?例如,如果我使用多个文件写入同一个文件,我应该在写入文件之前使用锁定还是仅使用临界区?

What is the difference between OpenMP locks and critical section? Are they just alternatives for each other? For example if I am writing to a same file using multiple files, should I use the locking or just the critical section before writing into the file?

推荐答案

关键部分最常在内部使用锁,例如:

Critical sections will most commonly use a lock internally, e.g.:

  • libgomp:来源
  • libiomp:

    • libgomp: source
    • libiomp:

      如果省略了可选的(名称),它会锁定一个未命名的全局互斥锁.

    • If the optional (name) is omitted, it locks an unnamed global mutex.

      >

      关键构造限制相关联的执行一次一个线程的结构化块

      The critical construct restricts execution of the associated structured block to a single thread at a time

      因此,临界区与获取锁的目的相同.不同之处在于为您处理低级细节.

      A critical section therefore serves the same purpose as acquiring a lock. The difference is that the low-level details are handled for you.

      由于简单,我建议您尽可能使用 critical.如果您有单独的块需要重要但不相互干扰,请为它们命名,并且仅当您需要一些注释无法容纳的行为时,才使用显式锁定.

      I would advise you to use critical whenever possible due to the simplicity. If you have separate blocks that need to be critical but don't interfere with each other give them names, and only if you need some behaviour that cannot be accommodated by the annotations, use explicit locking.

      这篇关于OpenMP 临界区与锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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