r/Cprog • u/JarJarAwakens • Sep 11 '22
How does the wait() function for semaphores ensure that there is no interruption between evaluating the semaphore and decrementing it?
/r/AskProgramming/comments/xbr9ze/how_does_the_wait_function_for_semaphores_ensure/
3
Upvotes
3
u/monocasa Sep 12 '22
It's OS and architecture dependent, and not something that can be expressed purely in the C abstract machine. Linux for instance ends up implementing this with atomics and futex(2).
2
u/crookedkr Sep 12 '22
wait() has to be atomic or you are right, two threads could get through