Parallel programming - Semaphore (Counter Variable)

About

A semaphore is a variable that control the access (physical or logical) by multiple thread to a common resource.

They are used for synchronization between two or more processes.

Example

Stoplight

If the stoplight is green, a train can enter the train station. If it is yellow or red (or any other color), the train station cannot be accessed.

Connection Pool

With an application that supports max 10 connection, a semaphore with the value 10 will be created.

  • Whenever the application creates a connection, the semaphore will be decremented by 1.
  • Whenever the application disconnect a connection, the semaphore will be incremented by 1.
  • When the semaphore is above 10, the application will wait or return a message.

Documentation / Reference


Powered by ComboStrap