in your C++ code you can likely remove the explicit load of cnt
before the futex_wait
call. The futex_wait
syscall internally performs a load of the futex word cnt
with sequential consistency. This ensures the atomicity of checking the value of the futex word and the blocking action as well as properly sequencing them, which is no different from std::memory_order_seq_cst
's guarantees.
why this works?
- Futex operations are ordered inside, and the load inside futex_wait
coordinates the memory synchronization as necessary.
- This eliminates the need for an explicit initial load, and your code is still correct and optimal.
so,
- do not use explicit load,
- assume sequential consistency of futex_wait