Semaphore :
Using a counter or flag to control access some shared resources in a concurrent system, implies use of Semaphore.
Example:
- A counter to allow only 50 Passengers to acquire the 50 seats (Shared resource) of any Theatre/Bus/Train/Fun ride/Classroom. And to allow a new Passenger only if someone vacates a seat.
- A binary flag indicating the free/occupied status of any Bathroom.
- Traffic lights are good example of flags. They control flow by regulating passage of vehicles on Roads (Shared resource)
Flags only reveal the current state of Resource, no count or any other information on the waiting or running objects on the resource.
Monitor :
A Monitor synchronizes access to an Object by communicating with threads interested in the object, asking them to acquire access or wait for some condition to become true.
Example:
- A Father may acts as a monitor for her daughter, allowing her to date only one guy at a time.
- A school teacher using baton to allow only one child to speak in the class.
- Lastly a technical one, transactions (via threads) on an Account object synchronized to maintain integrity.