Is there a full memory barrier around sem_post(sem

2020-02-11 05:32发布

问题:

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.

回答1:

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.



回答2:

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.



标签: c++ c linux