Libthreadar 1.4.0
Loading...
Searching...
No Matches
Public Member Functions | List of all members
libthreadar::semaphore Class Reference

Class semaphore is an enhanced version of Posix semaphore. More...

#include <semaphore.hpp>

Public Member Functions

 semaphore (unsigned int max_value)
 semaphore constuctor
 
 semaphore (const semaphore &ref)=delete
 no copy constructor
 
 semaphore (semaphore &&ref) noexcept=default
 no move constructor
 
semaphoreoperator= (const semaphore &ref)=delete
 no assignment operator
 
semaphoreoperator= (semaphore &&ref) noexcept=default
 no move operator
 
 ~semaphore ()
 Destructor.
 
bool waiting_thread () const
 Return whether the semaphore has at least a pending thread waiting for another thread to unlock it.
 
bool working_thread () const
 return whether the semaphore has at least one thread that acquired the lock, possibily without other thread pending
 
void lock ()
 Request a "resource".
 
void unlock ()
 Release a "resource".
 
void reset ()
 Reset to initial state releasing any thread that could wait on the semaphore.
 
int get_value () const
 Return the value of the semaphore, that's to say the number of available "resources".
 

Detailed Description

Class semaphore is an enhanced version of Posix semaphore.

In addition to Posix Semaphore, this class let the calling thread to detect whether other thread than the one currently having the lock are waiting for the semaphore to be released

Definition at line 52 of file semaphore.hpp.

Constructor & Destructor Documentation

◆ semaphore()

libthreadar::semaphore::semaphore ( unsigned int  max_value)

semaphore constuctor

Parameters
[in]max_valueis the maximum number of thread that can concurrently request wait() without being suspended
Note
the initial value is set to the max value, calling lock() can decrease this initial value at will, but calling unlock() which increases the semaphore value that must not lead it to exceed maximum value. In other words there must not be more unlock() calls than lock() calls invoked so far.
A thread calling lock() when the value is less than or equal to zero is suspended up to the time another thread calls unlock(). If more than one was pending on that semaphore, unlock() awakes a single thread.

Member Function Documentation

◆ get_value()

int libthreadar::semaphore::get_value ( ) const
inline

Return the value of the semaphore, that's to say the number of available "resources".

Note
a negative value gives the total number of threads that are suspended by calling lock() (well the opposit value of the number of threads more precisely).

Definition at line 109 of file semaphore.hpp.

◆ lock()

void libthreadar::semaphore::lock ( )

Request a "resource".

Note
at most max_value (given at construction time) "resources" can be requested at a time. if no more "resource" is available the caller is suspended waiting for another thread to release a resource calling unlock(). The notion of "resource" is an abstraction, that's up to the developer relying on that class to define what a "resource" is. The semaphore only assures that at most max_value resource will be used at the same time.

◆ unlock()

void libthreadar::semaphore::unlock ( )

Release a "resource".

Note
Note that if one or more thread are suspended due to a call to lock() a single thread is awaken and returns from the lock() call it was suspended into.

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