3 ms·
Luu says regarding the Little Book of Semaphores: "If I’m writing grungy low-level threading code today, I’m overwhelmingly like to be using c++11 threading
by adrianratnapala 10y ago
Luu says regarding the Little Book of Semaphores:
"If I’m writing grungy low-level threading
code today, I’m overwhelmingly like to
be using c++11 threading primitives, so
I’d like something that uses those instead
of semaphores,"
But as someone whose lowest-level experience of concurrency is with APIs similar to the C++ primitives (which are not that different from pthreads), I disagree. I found it a real eye-opener to see how all this can be broken down to semaphores.
I am starting to think that a semaphore-only set of primtives would be easier to reason about. I've seen better coders than me make over-complicated mutex based solutions when sempahores gave a simple answer. And I bet I've done it too.
- david-given 10y agoI totally agree. I've spent way too much time in the depths of terrible embedded operating systems trying to do basic synchronisation out of whatever terrible primitive they'd decided to implement instead of semaphores, when semaphores would be so much easier. (Frequently I ended up just implementing semaphores, and then doing everything else in terms of those, frequently using a standard library I had precisely for that purpose...) (Embedded device flashback moment: a mobile phone OS where the only concurrency primitive was a mutex, each of which contained a fixed-size buffer of waiting tasks. The size of the buffer was smaller than the number of tasks on the system, and if a task blocked on the mutex while the buffer was fill, another task would randomly wake up.)
- ktRolster 10y agoWhen you use pthread semaphores, be careful reading the documentation, because there are some gotchas that are not immediately obvious. I think that's why more programmers don't use them, because at first they seem intimidating.