简体   繁体   English

sem_post(sem_t * sem)和sem_wait(sem_t * sem)周围是否存在完整的内存屏障?

[英]Is there a full memory barrier around sem_post(sem_t * sem) and sem_wait(sem_t * sem)?

In linux code, I remember hearing there is a full memory barrier around mutex_lock(). 在linux代码中,我记得听说过mutex_lock()周围有完整的内存屏障。 I want to make sure whether it is around sem_xxx also. 我想确定它是否也在sem_xxx附近。

The authoritative answer is here: 权威答案在这里:

http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_11 http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_11

Applications shall ensure that access to any memory location by more than one thread of control (threads or processes) is restricted such that no thread of control can read or modify a memory location while another thread of control may be modifying it. 应用程序应确保限制一个以上控制线程(线程或进程)对任何内存位置的访问,以使任何控制线程都无法读取或修改一个内存位置,而另一个控制线程可以对其进行修改。 Such access is restricted using functions that synchronize thread execution and also synchronize memory with respect to other threads. 使用同步线程执行并相对于其他线程同步内存的函数来限制此类访问。 The following functions synchronize memory with respect to other threads: ... 以下功能使内存相对于其他线程同步:...

sem_wait and sem_post are in the list, so they are full memory barriers. sem_waitsem_post在列表中,因此它们是完整的内存屏障。

Yes, it uses an atomic increment/decrement in the uncontended case, which of course has a membar. 是的,在无竞争的情况下,它使用原子递增/递减,这当然有一个小数。 For the contended case there is a system call to futex, which also has a membar. 对于有争议的情况,有一个对futex的系统调用,它也有一个成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM