Libthreadar 1.6.0
Loading...
Searching...
No Matches
libthreadar::condition Class Reference

Wrapper around the Posix pthread_cond_t object and its associated mutex. More...

#include <condition.hpp>

Inherits libthreadar::mutex.

Public Member Functions

 condition (unsigned int num=1)
 constructor
 condition (const condition &ref)=delete
 no copy constructor
 condition (condition &&ref)=default
 no move constructor
conditionoperator= (const condition &ref)=delete
 no assignment operator
conditionoperator= (condition &&ref) noexcept=default
 no move operator
 ~condition ()
 destructor
void wait (unsigned int instance=0)
 put the calling thread on hold waiting for another thread to call signal()
void signal (unsigned int instance=0)
 awakes a single thread suspended for having called wait() on the condition given in argument
void broadcast (unsigned int instance=0)
 awakes all threads suspended for having called wait() on the condition given in argument
unsigned int get_waiting_thread_count (unsigned int instance=0) const
 return the number of thread currently waiting on that condition
Public Member Functions inherited from libthreadar::mutex
 mutex ()
 constructor
 mutex (const mutex &ref)=delete
 no copy constructor
 mutex (mutex &&ref)=default
 no move constructor
mutexoperator= (const mutex &ref)=delete
 no assignment operator
mutexoperator= (mutex &&ref) noexcept=default
 no move operator
virtual ~mutex ()
 destructor
void lock ()
 lock the mutex
void unlock ()
 unlock the mutex
bool try_lock ()
 Tells whether calling lock() would currently suspend the caller or not.

Additional Inherited Members

Protected Attributes inherited from libthreadar::mutex
pthread_mutex_t mut

Detailed Description

Wrapper around the Posix pthread_cond_t object and its associated mutex.

Class condition inherits from the mutex class and thus provides the lock(), unlock() and try_lock() methods. These must be used around the wait(), signal() or broadcast() methods brought by this class, see details for each of them.

Note
each object share a user defined set of condition on the same mutex as defined at constructor time. wait() takes as argument the condition number and will be awaken by a signal() or broadcast() having this same number as argument

Definition at line 49 of file condition.hpp.

Constructor & Destructor Documentation

◆ condition()

libthreadar::condition::condition ( unsigned int num = 1)

constructor

Parameters
[in]numnumber of instance to create, each instance is a separated condition relying on the same mutex. First instance starts with index 0

Referenced by condition(), condition(), operator=(), and operator=().

Member Function Documentation

◆ broadcast()

void libthreadar::condition::broadcast ( unsigned int instance = 0)

awakes all threads suspended for having called wait() on the condition given in argument

Parameters
[in]instancebroadcast threads that have been waiting on that instance
Note
broadcast() must be called between lock() and unlock(). This is only at the time unlock() is called that other threads can exit from their suspended state, by returning from their wait() call.

◆ get_waiting_thread_count()

unsigned int libthreadar::condition::get_waiting_thread_count ( unsigned int instance = 0) const
inline

return the number of thread currently waiting on that condition

Parameters
[in]instancethe condition instance number to count the waiting thread on

Definition at line 105 of file condition.hpp.

◆ signal()

void libthreadar::condition::signal ( unsigned int instance = 0)

awakes a single thread suspended for having called wait() on the condition given in argument

Parameters
[in]instancethe condition number to consider, only thread having called wait() with this same instance as argument are eligible to be awaken here.
Note
signal() must be called between lock() and unlock(). This is only at the time unlock() is called that another thread exits from the suspended state.

◆ wait()

void libthreadar::condition::wait ( unsigned int instance = 0)

put the calling thread on hold waiting for another thread to call signal()

Parameters
[in]instancethe instance number to have the caller waiting on
Note
wait() must be called between lock() and unlock(). Once suspendend calling wait(), the current object is transparently unlocked, for other thread to be able to use it. The mutex is re-acquires (transparently locked) once the thread exits the wait() call it was suspended on, which occurs if another thread calls signal() with the same instance number

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