如何休眠 C++ Boost 线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4265310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sleep a C++ Boost Thread
提问by Andry
Seems impossible to sleep a thread using boost::thread. Method sleep requires a system_time but how can I build it?
使用 boost::thread 使线程休眠似乎是不可能的。方法 sleep 需要 system_time 但我如何构建它?
Looking inside libraries doesn't really help much...
查看图书馆内部并没有太大帮助......
Basically I have a thread inside the function that I pass to this thread as entry point, I would like to call something like
基本上我在函数内部有一个线程作为入口点传递给这个线程,我想调用类似的东西
boost::this_thread::sleep
or something, how to do this?
或者什么,如何做到这一点?
Thank you
谢谢
回答by Benjamin Lindley
Depending on your version of Boost:
根据您的 Boost 版本:
Either...
任何一个...
#include <boost/chrono.hpp>
#include <boost/thread/thread.hpp>
boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
Or...
或者...
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/thread.hpp>
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
You can also use microseconds, seconds, minutes, hours and maybe some others, I'm not sure.
您还可以使用微秒、秒、分钟、小时和其他一些,我不确定。
回答by user3731622
From another post, I learned boost::this_thread::sleep
is deprecated for Boost v1.5.3: http://www.boost.org/doc/libs/1_53_0/doc/html/thread/thread_management.html
从另一篇文章中,我了解到boost::this_thread::sleep
Boost v1.5.3 已被弃用:http: //www.boost.org/doc/libs/1_53_0/doc/html/thread/thread_management.html
Instead, try
相反,尝试
void sleep_for(const chrono::duration<Rep, Period>& rel_time);
e.g.
例如
boost::this_thread::sleep_for(boost::chrono::seconds(60));
Or maybe try
或者试试
void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
I was using Boost v1.53 with the deprecated sleep
function, and it aperiodically crashed the program. When I changed calls to the sleep
function to calls to the sleep_for
function, the program stopped crashing.
我使用的是带有过时sleep
功能的Boost v1.53 ,它不定期地使程序崩溃。当我将对该sleep
函数的调用更改为对该函数的调用时sleep_for
,程序停止崩溃。
回答by user2749562
firstly
首先
boost::posix_time::seconds secTime(1);
boost::this_thread::sleep(secTime);
secondly
其次
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
回答by Lenny
I learned the hard way that at least in MS Visual Studio (tried 2013 and 2015) there is the huge difference between
我学到了至少在 MS Visual Studio(尝试过 2013 年和 2015 年)之间的巨大差异
boost::this_thread::sleep(boost::posix_time::microseconds(SmallIterval));
and
和
boost::this_thread::sleep_for(boost::chrono::microseconds(SmallIterval));
or
std::this_thread::sleep_for(std::chrono::microseconds(SmallIterval));
when interval is smaller than some rather substantial threshold (I saw threshold of 15000 microseconds = 15 milliseconds).
当间隔小于某个相当大的阈值时(我看到阈值为 15000 微秒 = 15 毫秒)。
If SmallIterval is small, sleep() does instantaneous interruption. sleep(100 mks) behaves as sleep(0 mks).
如果 SmallIterval 很小, sleep() 会进行瞬时中断。sleep(100 mks) 表现为 sleep(0 mks)。
But sleep_for() for the time interval smaller than a threshold pauses for the entire threshold. sleep_for(100 mks) behaves as sleep_for(15000 mks).
但是对于小于阈值的时间间隔的 sleep_for() 会暂停整个阈值。sleep_for(100 mks) 的行为与 sleep_for(15000 mks) 相同。
Behavior for intervals larger than threshold and for value 0 is the same.
间隔大于阈值和值 0 的行为是相同的。