Loading...
Searching...
No Matches
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
semaphore & | operator= (const semaphore &ref)=delete
| no assignment operator
semaphore & | operator= (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 able to know 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_value | is the maximum number of thread that can concurrently call lock() without being suspended

Note
  the initial value is set to the max value, calling lock() decrease the semaphore value by one, this can be done at will, but calling unlock() which increases the semaphore value, 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. For example, if the value is one before calling lock() the thread will not be suspended, but the next thread calling lock() now that the value is zero, will be suspended. If more than one thread was pending on a semaphore, unlock() awakes a single thread (It must not be assumed that it will be the oldest waiting on this semaphore).

## 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 111 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 by 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:

- semaphore.hpp