Semaphores are a generalization of mutexes. Maybe it’s better to state the opposite: Mutexes are a special case of the semaphore primitive invented by the Dutch computer scientist Dijkstra. The name semaphore was inspired by synchronization mechanisms used in railways; they are the equivalent to roadside traffic lights.
Before getting into details about the
Semaphore class and its properties, let’s take a look at a railway semaphore. Figure 29.23 shows states a semaphore can be in, which are used to communicate actions to train drivers. I’m not an expert in railway design, but the basic idea is simple. One sign indicates the train has to stop, and another indicates it’s safe to pass. An intermediate state sometimes exists, too, indicating ...