[http://blog.csdn.net/taku2006/article/details/78678573]前一篇主要介绍了thread类,这篇主要介绍condition_variable
1. condtion_variable类
函数名 | 函数形式 | 备注 |
---|---|---|
构造器 | condition_variable(); | 不支持拷贝,移动构造器 |
析构器 | ~condition_variable(); | 1.调用析构器之前,所有block在该object上的thread均应被notified. 2.否则将产生不可预知行为 |
wait | -void wait (unique_lock<mutex>& lck); -template <class Predicate> void wait (unique_lock<mutex>& lck, Predicate pred); |
wait until notified |
wait for | template <class Rep, class Period> cv_status wait_for (unique_lock<mutex>& lck, const chrono::duration<Rep,Period>& rel_time); template <class Rep, class Period, class Predicate> bool wait_for (unique_lock& lck, const chrono::duration<Rep,Period>& rel_time, Predicate pred); |
wait for timeout or until notified |
wait until | template <class Clock, class Duration> cv_status wait_until (unique_lock<mutex>& lck, const chrono::time_point<Clock,Duration>& abs_time); template <class Clock, class Duration, class Predicate> bool wait_until (unique_lock<mutex>& lck, const chrono::time_point<Clock,Duration>& abs_time,Predicate pred); |
Wait until notified or time point |
notify | void notify_one() noexcept; void notify_all() noexcept; |
唤醒 |
condition_variable类还有个相似的类,condition_variable_any
两个类的区别在于:
-condition_variable类只能使用unique_lock<mutex> 来lock thread。
-condition_variable_any类可以使用任意lockable type
1. 代码实践
1.1 实例1
int Num =