semaphore

A synchronization primitive that uses a counter to manage access to a pool of shared resources.

A semaphore is a more flexible synchronization tool than a mutex. It maintains a counter representing the number of available resources. The 'wait' operation decrements the counter, and the 'signal' (or post) operation increments it. A binary semaphore (counter 0 or 1) functions similarly to a mutex.

        graph LR
  Center["semaphore"]:::main
  Rel_mutex["mutex"]:::related -.-> Center
  click Rel_mutex "/terms/mutex"
  classDef main fill:#7c3aed,stroke:#8b5cf6,stroke-width:2px,color:white,font-weight:bold,rx:5,ry:5;
  classDef pre fill:#0f172a,stroke:#3b82f6,color:#94a3b8,rx:5,ry:5;
  classDef child fill:#0f172a,stroke:#10b981,color:#94a3b8,rx:5,ry:5;
  classDef related fill:#0f172a,stroke:#8b5cf6,stroke-dasharray: 5 5,color:#94a3b8,rx:5,ry:5;
  linkStyle default stroke:#4b5563,stroke-width:2px;

      

🧠 Knowledge Check

1 / 1

🧒 Explain Like I'm 5

A semaphore is like a bouncer at a nightclub with limited capacity. If the club can hold 50 people, the bouncer lets people in until it's full. Once it's full, new people have to wait in line until someone leaves.

🤓 Expert Deep Dive

Counting semaphores are used for resource pools (like database connection pools). The 'p' (proberen) and 'v' (verhogen) operations must be atomic. Semaphores can be used for task synchronization (signaling one thread when another finishes) in addition to mutual exclusion.

📚 Sources