Condition Variable vs Semaphore¶
Semaphore¶
Worldview:
resource synchronization
Example:
State exists inside semaphore.
Condition Variable¶
Worldview:
condition synchronization
Example:
State exists outside condvar.
Predicate Loop¶
Correct:
Wrong:
Reason:
- spurious wakeup
- broadcast
- race avoidance
Comparison¶
| Item | Semaphore | Condvar |
|---|---|---|
| Store count | Yes | No |
| Store state | Internal | External |
| Queue | Excellent | Good |
| State machine | Moderate | Excellent |
| Timeout | Yes | Yes |
Conclusion¶
Semaphore waits for resources.
Condition variable waits for state transitions.