tlx
Semaphore Class Reference

A simple semaphore implementation using C++11 synchronization methods. More...

#include <semaphore.hpp>

Public Member Functions

 Semaphore (size_t initial_value=0)
 construct semaphore More...
 
 Semaphore (const Semaphore &)=delete
 non-copyable: delete copy-constructor More...
 
Semaphoreoperator= (const Semaphore &)=delete
 non-copyable: delete assignment operator More...
 
 Semaphore (Semaphore &&s)
 move-constructor: just move the value More...
 
Semaphoreoperator= (Semaphore &&s)
 move-assignment: just move the value More...
 
size_t signal ()
 function increments the semaphore and signals any threads that are blocked waiting a change in the semaphore More...
 
size_t signal (size_t delta)
 function increments the semaphore and signals any threads that are blocked waiting a change in the semaphore More...
 
size_t wait (size_t delta=1, size_t slack=0)
 function decrements the semaphore by delta and blocks if the semaphore is < (delta + slack) until another thread signals a change More...
 
bool try_acquire (size_t delta=1, size_t slack=0)
 function decrements the semaphore by delta if (delta + slack) tokens are available as a batch. More...
 
size_t value () const
 return the current value – should only be used for debugging. More...
 

Private Attributes

size_t value_
 value of the semaphore More...
 
std::mutex mutex_
 mutex for condition variable More...
 
std::condition_variable cv_
 condition variable More...
 

Detailed Description

A simple semaphore implementation using C++11 synchronization methods.

Definition at line 26 of file semaphore.hpp.

Constructor & Destructor Documentation

Semaphore ( size_t  initial_value = 0)
inlineexplicit

construct semaphore

Definition at line 30 of file semaphore.hpp.

Semaphore ( const Semaphore )
delete

non-copyable: delete copy-constructor

Semaphore ( Semaphore &&  s)
inline

move-constructor: just move the value

Definition at line 38 of file semaphore.hpp.

Member Function Documentation

Semaphore& operator= ( const Semaphore )
delete

non-copyable: delete assignment operator

Semaphore& operator= ( Semaphore &&  s)
inline

move-assignment: just move the value

Definition at line 40 of file semaphore.hpp.

size_t signal ( )
inline

function increments the semaphore and signals any threads that are blocked waiting a change in the semaphore

Definition at line 44 of file semaphore.hpp.

size_t signal ( size_t  delta)
inline

function increments the semaphore and signals any threads that are blocked waiting a change in the semaphore

Definition at line 52 of file semaphore.hpp.

bool try_acquire ( size_t  delta = 1,
size_t  slack = 0 
)
inline

function decrements the semaphore by delta if (delta + slack) tokens are available as a batch.

the function will not block and returns true if delta was acquired otherwise false.

Definition at line 70 of file semaphore.hpp.

size_t value ( ) const
inline

return the current value – should only be used for debugging.

Definition at line 79 of file semaphore.hpp.

size_t wait ( size_t  delta = 1,
size_t  slack = 0 
)
inline

function decrements the semaphore by delta and blocks if the semaphore is < (delta + slack) until another thread signals a change

Definition at line 60 of file semaphore.hpp.

Member Data Documentation

std::condition_variable cv_
private

condition variable

Definition at line 89 of file semaphore.hpp.

std::mutex mutex_
private

mutex for condition variable

Definition at line 86 of file semaphore.hpp.

size_t value_
private

value of the semaphore

Definition at line 83 of file semaphore.hpp.


The documentation for this class was generated from the following file: