43.5 Condition Variables

A condition variable is a mixed synchronization primitive that includes features of both a mutex and a semaphore. A condition variables is useful for check-pointing complex situations—especially where threads are permitted to access a resource based on a change in some condition. As long as the condition is expressible in C, it can be used to implement a condition variable. A condition variable uses a mutex to implement the sleeping point for the threads that must not access the protected resource.

For example, you could use condition variables to implement a producer/consumer or work crew component where a number of threads are producing work that consuming threads use so that neither type (producer or consumer) gets too far ahead or behind.

LibC supports condition variables on the NKS, pthreads, and UI interfaces. For a list of the various functions, see Condition Variable Interface Comparison.