域名 和网站有什么区别,天马网络网站,企业网页设计,详细描述建设网站场景
在开发多线程程序时#xff0c;有时候需要启动一个线程来监听外部进程的执行情况#xff0c;并且在指定时间如果还没运行结束就强制结束外部线程。那么C标准库有这种监听线程并能在超时时提示的方法吗#xff1f;
说明
在C11的condition_variable里就可以用…场景
在开发多线程程序时有时候需要启动一个线程来监听外部进程的执行情况并且在指定时间如果还没运行结束就强制结束外部线程。那么C标准库有这种监听线程并能在超时时提示的方法吗
说明
在C11的condition_variable里就可以用条件变量来等待信号通知, 并设置超时时间。 超时时间的含义是wait_for在超时达到时会自动唤醒mutex并不断尝试获取锁当锁被获取时进入下一条代码。
template class Lock, class Rep, class Period std::cv_status wait_for( Lock lock,const std::chrono::durationRep, 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::durationRep, 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 variables 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 chronousing 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::durationdouble 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::durationdouble 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