【发布时间】:2011-07-04 12:45:38
【问题描述】:
所以我创建了一些代码。我使用增强计时器。在这里
while(1){
timerForCaptureFame.restart();
//some code
spendedTimeForCaptureFame = timerForCaptureFame.elapsed();
if(spendedTimeForCaptureFame < desiredTimeForCaptureFame){
boost::this_thread::sleep(boost::posix_time::milliseconds(desiredTimeForCaptureFame - spendedTimeForCaptureFame));
}
}
是否会发生这样desiredTimeForCaptureFame - spendedTimeForCaptureFame 将 > 0 但 boost 会将其视为 0 并暂停线程?
【问题讨论】:
-
不 - 在此线程开始之前设置所需的时间。
-
我的意思是问题是 - 线程工作了一段时间而不是进入睡眠......
-
删除了我原来的评论,现在看起来你正在使用 boost::timer 所以你正在处理双打。
-
您正在从
double转换为long,所以是的,您会有舍入错误。另外 boost::timer 它不是很准确(它测量秒数),因此在转换为long时你更有可能得到 0。您可以执行long elapsedTime = 1000 * static_cast< long >( desiredTimeForCaptureFame - spendedTimeForCaptureFame ); if( elapsedTime > 0 ){ sleep(...); }之类的操作。我不太确定的是this_thread::sleep( 0 );是否永远沉睡......
标签: c++ multithreading boost time sleep