cpp-pthread
(v1.7.3)
Simple C++ wrapper to pthread functions.
|
#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 |
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.
Definition at line 56 of file condition_variable.hpp.
|
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.
|
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.
mtx | ralated mutex, which must be locked by the current thread. |
Definition at line 5 of file condition_variable.cpp.
void pthread::condition_variable::wait | ( | lock_guard< pthread::mutex > | lck | ) |
Definition at line 9 of file condition_variable.cpp.
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();
mtx | ralated mutex, which must be locked by the current thread. |
lambda | run to check if condition was met. |
Definition at line 209 of file condition_variable.hpp.
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();
lck | ralated mutex lock_guard, which must be locked by the current thread. |
lambda | run to check if condition was met. |
Definition at line 221 of file condition_variable.hpp.
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 );
mtx | ralated mutex, which must be locked by the current thread. |
millis | milliseconds to wait for this instance to signaled. |
condition_variable_exception | is thrown either if timeout calculation failed or mutex ownership was wrong. |
Definition at line 20 of file condition_variable.cpp.
cv_status pthread::condition_variable::wait_for | ( | lock_guard< pthread::mutex > & | lck, |
int | millis | ||
) |
Definition at line 14 of file condition_variable.cpp.
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();
mtx | ralated mutex, which must be locked by the current thread. |
millis | milli seconds to wait for condition to be signaled. |
lambda | run to check if condition was met. |
Definition at line 227 of file condition_variable.hpp.
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();
lck | ralated mutex lock_guard, which must be locked by the current thread. |
millis | milli seconds to wait for condition to be signaled. |
lambda | run to check if condition was met. |
Definition at line 263 of file condition_variable.hpp.