Contents
Semaphore

Overview

A traditional semaphore synchronisation object.

The semaphore maintains a set of permits that are created via increment calls on the semaphore, and consumed by decrement calls.

If there is no permit available when a decrement call is made, the calling thread blocks until a permit is created via an increment call.

Quick start

#include <Balau/Concurrent/Semaphore.hpp>

Using the semaphore is simple.

			// Create a semaphore.
			Semaphore semaphore;
		

An initial permit count can be specified at construction time if desired.

			// Create a semaphore with an initial permit count of 4.
			Semaphore semaphore(4);
		

Then each participating thread may call increment and/or decrement accordingly.

			///// Thread A /////

			/// Decrement the semaphore, blocking if the count is 0.
			semaphore.decrement();
		
			///// Thread B /////

			/// Increment the semaphore, adding a permit.
			semaphore.increment();