by ComputerGuru · 3y ago · 0 comments
https://neosmart.net/blog/2022/implementing-truly-safe-semap...
> there’s not much theoretical benefit to such a binary semaphore over a mutex – in fact, they’re interchangeable
Not interchangeable!
A binary semaphore has a superpower over a mutex: the lock-taker ("decrementer") can be disconnected from the lock-releaser ("incrementer"). You may wait for a thing and later be notified of the thing, but the notification comes on another thread, or in a signal handler, etc. But mutexes cannot handle locking in one thread and being unlocked somewhere else. Binary semaphores handle this fine; in this way they share something in common with condvars and also with mutexes.
The article calls this the "ugly part": how do you handle release()? But that's the source of a semaphore's power!
If you don't mean "in its spec" but "in practice, as written and most commonly used" then the pthread mutex fits that definition, at least under Linux, as noted in a footnote in the article:
> You can see a sample application testing for pthread_mutex_unlock() safety
> here [0] and try it out for yourself online here [1].
[0]: https://gist.github.com/mqudsi/5aac71d8177866ba2762bda01a3ff...
[1]: https://onlinegdb.com/9hmNhBuEc
You can literally use it as a binary semaphore, but I can't think of a good reason to do so since pthread semaphores are signal safe while pthread mutexes aren't (which is why many people do the opposite).
The only way it can not work that way is to keep track of the owning thread id, which it elides as an optimization and trusts the caller to "use it wisely" instead.
Using PTHREAD_MUTEX_ERRORCHECK instead of PTHREAD_MUTEX_NORMAL (which pretty much every platform I've seen defaults to as the value of PTHREAD_MUTEX_DEFAULT) causes that to fail, but when's the last time you ran into a project using PTHREAD_MUTEX_ERRORCHECK? (PTHREAD_MUTEX_RECURSIVE technically has the information required to also validate the unlocking thread, but I haven't tried to see if does in practice.)
This.
For me, a semaphore is a general way handle states and dependancies with synchronization.
Whether it's a file named ".lock" (like a simple traffic signal) or some fancy IPC (like a 4 way intersection between a segment with heavy traffic that gets the green light 99% of time, and one with low traffic, with cameras to detect cars on the small road for the 1% it will be useful, and pedestrian crossings with buttons to request a red ligh), it boils down to the same thing: enforcing states, with synchronization.
I'll make you laugh, but recently I used environment variables (!!) to do some basic semaphore without even a .lock file!
It may be ugly, but I needed something simple that would work on Windows and Linux. It did the job!
To store integers like "5" with environment variables, I use export VARIABLE=5
That way you can wait for n threads to finish work.
Blocks until n threads or n tasks have completed. Latter is useful in a threadpool context.