Semaphore
-
class Semaphore
A classic semaphore synchronization primitive.
A semaphore manages an internal counter which is decremented by each
acquire()
call and incremented by eachrelease()
call. The counter can never go below zero; whenacquire()
finds that it is zero, it blocks, waiting until some other thread callsrelease()
.Inheritance diagram
-
explicit Semaphore(int initial_count = 1)
-
void acquire(void)
Decrements the internal count. If the count was already at zero, blocks until the count is nonzero, then decrements it.
-
int get_count(void) const
Returns the current semaphore count. Note that this call is not thread- safe (the count may change at any time).
-
void output(std::ostream &out) const
-
int release(void)
Increments the semaphore’s internal count. This may wake up another thread blocked on
acquire()
.Returns the count of the semaphore upon release.
-
bool try_acquire(void)
If the semaphore can be acquired without blocking, does so and returns true. Otherwise, returns false.
-
explicit Semaphore(int initial_count = 1)