[C++11]_[初级]_[工作线程如何监听主线程条件变量wait_for方法的使用]
场景
- 在开发多线程程序时,有时候需要启动一个线程来监听外部进程的执行情况,并且在指定时间如果还没运行结束就强制结束外部线程。那么
C++
标准库有这种监听线程并能在超时时提示的方法吗?
说明
- 在
C++11
的<condition_variable>
里就可以用条件变量来等待信号通知, 并设置超时时间。 超时时间的含义是,wait_for
在超时达到时会自动唤醒mutex
并不断尝试获取锁,当锁被获取时,进入下一条代码。
template< class Lock, class Rep, class Period >std::cv_status wait_for( Lock& lock,const std::chrono::duration<Rep, Period>& rel_time );
mutex.lock();
cond.wait_for(mutex, std::chrono::seconds(10));
mutex.unlock();
wait_for
的重载函数还有一个返回值是bool
的函数对象。这个函数对象是为了避免虚假的唤醒,比如被错误的notify_one
唤醒时,需要判断是否返回true
, 如果返回true
, 那么结束等待,否则继续等到到超时。 这个带Predicate
参数的实现等同于
wait_until(lock, std::chrono::steady_clock::now() + rel_time, std::move(pred));.
[1]
template< class Lock, class Rep, class Period, class Predicate >bool wait_for( Lock& lock, const std::chrono::duration<Rep, Period>& rel_time,Predicate pred );
- 以上的
Predicate
函数对象的作用
等同于以下的实现:[2].
- 进入等待前先判断
Predicate
是否为true
, 如果为false
,进度等待。 - 如果等到超时,直接返回
Predicate
的值,即执行下一行代码。 - 如果非超时的唤醒,那么返回第一步。
while (!pred())if (wait_until(lock, abs_time) == std::cv_status::timeout)return pred();
return true;.
- 注意,使用条件变量唤醒时,不需要加锁。
cond1.notify_one();
[3]因为如果对通知操作进行加锁,那么通知发生时,等待线程唤醒后会尝试获取锁,但是获取不到会迅速阻塞,因为被通知的线程需要等待通知线程解锁。
The notifying thread does not need to hold the lock on the same mutex as the one held by the waiting thread(s);
in fact doing so is a pessimization, since the notified thread would immediately block again,
waiting for the notifying thread to release the lock.
However, some implementations (in particular many implementations of pthreads) recognize this situation and
avoid this "hurry up and
wait" scenario by transferring the waiting thread from the condition variable's queue directly to the queue of the mutex within the notify call, without waking it up.
例子
// test-wait-for.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <condition_variable>
#include <thread>
#include <mutex>
#include <chrono>using namespace std;void work_1(condition_variable_any& cond,mutex& mutex)
{mutex.lock();cond.wait_for(mutex, std::chrono::seconds(10));mutex.unlock();
}void TestWaitFor_1()
{condition_variable_any cond1;mutex mutex1;auto beg = std::chrono::system_clock::now();thread t1(work_1,std::ref(cond1),std::ref(mutex1));std::this_thread::sleep_for(std::chrono::seconds(3));//cond1.notify_one();t1.join();std::chrono::duration<double> seconds = std::chrono::system_clock::now() - beg;printf("TestWaitFor_2 elapsed_seconds: %fs\n", seconds);
}void work_2(condition_variable_any& cond,mutex& mutex,bool& bFinish)
{mutex.lock();cond.wait_for(mutex, std::chrono::seconds(10), [&bFinish]{return bFinish; });mutex.unlock();
}void TestWaitFor_2(bool bValue)
{condition_variable_any cond1;mutex mutex1;bool bFinish = false;auto beg = std::chrono::system_clock::now();thread t1(work_2,std::ref(cond1),std::ref(mutex1),std::ref(bFinish));std::this_thread::sleep_for(std::chrono::seconds(3));// 用来处理虚假的唤醒,即如果没有到timeout就收到唤醒且bFinish还为false的话,wait会继续。bFinish = bValue; // 想让notify_one的唤醒生效,必须bFinish = true;cond1.notify_one();t1.join();std::chrono::duration<double> seconds = std::chrono::system_clock::now() - beg;printf("TestWaitFor_2 elapsed_seconds: %fs\n", seconds);
}int main()
{std::cout << "Hello World!\n";std::cout << "\n==== TestWaitFor_1 ====" << endl;TestWaitFor_1();std::cout << "\n=== TestWaitFor_2 ==== bFinish = false" << endl;TestWaitFor_2(false);std::cout << "\n=== TestWaitFor_2 ==== bFinish = true" << endl;TestWaitFor_2(true);
}
输出
Hello World!==== TestWaitFor_1 ====
TestWaitFor_2 elapsed_seconds: 10.012182s=== TestWaitFor_2 ==== bFinish = false
TestWaitFor_2 elapsed_seconds: 10.008821s=== TestWaitFor_2 ==== bFinish = true
TestWaitFor_2 elapsed_seconds: 3.030650s
参考
-
wait_for
-
wait_util
-
notify_one
-
pthread_cond_signal