This provides an alternate implementation of posix condition variables, based only on posix semaphores.
They can be used instead of regular pthread_cond_t
objects subject to the
following restrictions:
- They don't obey any non-default
pthread_condattr_t
attributes - They don't provide cancellation points.
- They cannot be initialized with a static initializer
Two versions are provided, the weak and the strong condition variables. This terminology is my own.
-
The weak version relaxes one of the classic condition cariable promises, namely that one of the treads already waiting gets awoken by a
pthread_cond_signal()
(or all of them in case of apthread_cond_broadcast()
), and instead, guarantees only that a thread will wake up. I.e. it is possible that a thread which has not yet started to wait, will be the one woken up. -
At the cost of some sligt added complexity, the strong version will uphold the aforementioned promise.
Interestingly, tests on multicore machines show that the both versions outperform
the regular pthread_cond_t
primitives, and the strong type results in much lower
latency.
This could be used as a drop-in replacement in many multithreaded programs where latency is of prime importance.