cpp-pthread  (v1.7.3)
Simple C++ wrapper to pthread functions.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
Public Member Functions | List of all members
pthread::condition_variable Class Reference

#include <condition_variable.hpp>

Public Member Functions

void wait (mutex &mtx)
 
void wait (lock_guard< pthread::mutex > lck)
 
template<class Lambda >
bool wait (mutex &mtx, Lambda lambda)
 
template<class Lambda >
bool wait (lock_guard< pthread::mutex > &lck, Lambda lambda)
 
cv_status wait_for (mutex &mtx, int millis)
 
cv_status wait_for (lock_guard< pthread::mutex > &lck, int millis)
 
template<class Lambda >
bool wait_for (mutex &mtx, int millis, Lambda lambda)
 
template<class Lambda >
bool wait_for (lock_guard< pthread::mutex > &lck, int millis, Lambda lambda)
 
void notify_one () noexcept
 
void notify_all () noexcept
 

Detailed Description

Condition variable.

The condition_variable class is a synchronization primitive that can be used to block a thread, or multiple threads at the same time, until another thread both modifies a shared variable (the condition), and notifies the condition_variable.

The thread that intends to modify the variable has to

Even if the shared variable is atomic, it must be modified under the mutex in order to correctly publish the modification to the waiting thread.

Upon successful return, the mutex shall have been locked and shall be owned by the calling thread.

Author
herbert koelman (herbe.nosp@m.rt.k.nosp@m.oelma.nosp@m.n@me.nosp@m..com)

Definition at line 56 of file condition_variable.hpp.

Member Function Documentation

void pthread::condition_variable::notify_all ( )
noexcept

Signal all waiting threads.

The call unblocks all threads currently blocked on the specified condition variable cond.

Definition at line 61 of file condition_variable.cpp.

void pthread::condition_variable::notify_one ( )
noexcept

Signal one waiting thread.

The call unblocks at least one of the threads that are blocked on the specified condition variable cond (if any threads are blocked on cond).

Definition at line 53 of file condition_variable.cpp.

void pthread::condition_variable::wait ( mutex mtx)

Wait for condition to be signaled.

This method atomically release mutex and cause the calling thread to block; atomically here means "atomically with respect to access by another thread to the mutex and then the condition variable". Call notify_one or notify_all to signal the condition.

Upon successful return, the mutex has been locked and is owned by the calling thread.

Parameters
mtxralated mutex, which must be locked by the current thread.
See also
notify_one
notify_all

Definition at line 5 of file condition_variable.cpp.

void pthread::condition_variable::wait ( lock_guard< pthread::mutex lck)
See also
wai

Definition at line 9 of file condition_variable.cpp.

template<class Lambda >
bool pthread::condition_variable::wait ( mutex mtx,
Lambda  lambda 
)

Wait for condition to be signaled.

This method atomically release mutex and cause the calling thread to block; atomically here means "atomically with respect to access by another thread to the mutex and then the condition variable". Call notify_one or notify_all to signal the condition.

Upon successful return, the mutex has been locked and is owned by the calling thread.

The lambda (closure) is run to check if the condition was met. Lambda should return false if the waiting should be continued. The signature of the predicate function should be equivalent to the following: bool pred();

Parameters
mtxralated mutex, which must be locked by the current thread.
lambdarun to check if condition was met.
Returns
true if lmabda returned true.
See also
notify_one
notify_all

Definition at line 209 of file condition_variable.hpp.

template<class Lambda >
bool pthread::condition_variable::wait ( lock_guard< pthread::mutex > &  lck,
Lambda  lambda 
)

Wait for condition to be signaled.

This method atomically release mutex and cause the calling thread to block; atomically here means "atomically with respect to access by another thread to the mutex and then the condition variable". Call notify_one or notify_all to signal the condition.

Upon successful return, the mutex has been locked and is owned by the calling thread.

The lambda (closure) is run to check if the condition was met. Lambda should return false if the waiting should be continued. The signature of the predicate function should be equivalent to the following: bool pred();

Parameters
lckralated mutex lock_guard, which must be locked by the current thread.
lambdarun to check if condition was met.
Returns
true if lmabda returned true.
See also
notify_one
notify_all

Definition at line 221 of file condition_variable.hpp.

cv_status pthread::condition_variable::wait_for ( mutex mtx,
int  millis 
)

Wait for condition to be signaled within given time frame.

This method atomically release mutex and cause the calling thread to block; atomically here means "atomically with respect to access by another thread to the mutex and then the condition variable". Call notify_one or notify_all to signal the condition.

Upon successful return, the mutex has been locked and is owned by the calling thread.

If this method is called with millis < 0 then the timeout time is not recalculated. This make it possible to handle spurious unblocking of condition variable without the need of a lambda expression. The call sequence is then: while(! check_condition() && wait_for(lck, 200) == no_tiemout );

Parameters
mtxralated mutex, which must be locked by the current thread.
millismilliseconds to wait for this instance to signaled.
Returns
cv_status (either timeout or no_timeout)
Exceptions
condition_variable_exceptionis thrown either if timeout calculation failed or mutex ownership was wrong.
See also
notify_one
notify_all

Definition at line 20 of file condition_variable.cpp.

cv_status pthread::condition_variable::wait_for ( lock_guard< pthread::mutex > &  lck,
int  millis 
)
See also
wait_for (mutex &, int)

Definition at line 14 of file condition_variable.cpp.

template<class Lambda >
bool pthread::condition_variable::wait_for ( mutex mtx,
int  millis,
Lambda  lambda 
)

Wait for condition to be signaled within a given time frame.

This method atomically release mutex and cause the calling thread to block; atomically here means "atomically with respect to access by another thread to the mutex and then the condition variable". Call notify_one or notify_all to signal the condition.

Upon successful return, the mutex has been locked and is owned by the calling thread.

The lambda (closure) is run to check if the condition was met. Lambda should return false if the waiting should be continued. The signature of the predicate function should be equivalent to the following: bool lambda();

Parameters
mtxralated mutex, which must be locked by the current thread.
millismilli seconds to wait for condition to be signaled.
lambdarun to check if condition was met.
Returns
true if lmabda returned true.
See also
notify_one
notify_all

Definition at line 227 of file condition_variable.hpp.

template<class Lambda >
bool pthread::condition_variable::wait_for ( lock_guard< pthread::mutex > &  lck,
int  millis,
Lambda  lambda 
)

Wait for condition to be signaled within a given time frame.

This method atomically release mutex and cause the calling thread to block; atomically here means "atomically with respect to access by another thread to the mutex and then the condition variable". Call notify_one or notify_all to signal the condition.

Upon successful return, the mutex has been locked and is owned by the calling thread.

The lambda (closure) is run to check if the condition was met. Lambda should return false if the waiting should be continued. The signature of the predicate function should be equivalent to the following: bool lambda();

Parameters
lckralated mutex lock_guard, which must be locked by the current thread.
millismilli seconds to wait for condition to be signaled.
lambdarun to check if condition was met.
Returns
true if lmabda returned true.
See also
notify_one
notify_all

Definition at line 263 of file condition_variable.hpp.


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