Is there a full memory barrier around sem_post(sem

2020-02-11 05:27发布

In linux code, I remember hearing there is a full memory barrier around mutex_lock(). I want to make sure whether it is around sem_xxx also.

标签: c++ c linux
2条回答
Evening l夕情丶
2楼-- · 2020-02-11 06:02

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.

查看更多
可以哭但决不认输i
3楼-- · 2020-02-11 06:06

The authoritative answer is here:

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.

查看更多
登录 后发表回答