关于pthread互斥锁所涉及的完整内存障碍的说明 [英] clarifications on full memory barriers involved by pthread mutexes

查看:101
本文介绍了关于pthread互斥锁所涉及的完整内存障碍的说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说在处理互斥锁时,必需的内存屏障由pthread API本身处理.我想在这件事上有更多细节.

I have heard that when dealing with mutexes, the necessary memory barriers are handled by the pthread API itself. I would like to have more details on this matter.

  1. 至少在周围最常见的架构上,这些说法是否正确?
  2. 编译器是否能够识别此隐式障碍,并避免在生成代码时对操作/从本地寄存器读取的操作进行重新排序?
  3. 何时应用内存屏障:成功获取互斥锁后并释放互斥锁?

推荐答案

POSIX规范列出了必须,其中包括pthread_mutex_lock()pthread_mutex_unlock()之类的功能.

The POSIX specification lists the functions that must "synchronize memory with respect to other threads", which includes functions like pthread_mutex_lock() and pthread_mutex_unlock().

附录A.4.11 中,明确指出同步内存"的功能:

In Appendix A.4.11 it is spelt out that functions that "synchronize memory":

  • ...将必须被高级编译系统识别,以使内存操作和对这些函数的调用不会因优化而重新排序;和

  • ...would have to be recognized by advanced compilation systems so that memory operations and calls to these functions are not reordered by optimization; and

...根据特定的计算机,可能必须添加内存同步指令.

...would potentially have to have memory synchronization instructions added, depending on the particular machine.

从来没有明确指定隐含哪种内存同步指令-隐式规范是,如果您使用一对同步指令"来确保一个线程中的一个读取必须在另一个线程中的写入之后发生,则您的程序将正常运行.这包括编译器和体系结构的重新排序效果.

It is never explicitly specified what kind of memory synchronization instructions are implied - the implicit specification is that if you use a pair of "synchronizing instructions" to ensure that a read in one thread must happen after a write in the other, then your program will operate correctly. This includes both compiler and architectural reordering effects.

这篇关于关于pthread互斥锁所涉及的完整内存障碍的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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