【问题标题】:Can boost boost::posix_time::milliseconds turn value near to 0 into 0?boost boost::posix_time::milliseconds 可以将接近 0 的值变成 0 吗?
【发布时间】: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&lt; long &gt;( desiredTimeForCaptureFame - spendedTimeForCaptureFame ); if( elapsedTime &gt; 0 ){ sleep(...); } 之类的操作。我不太确定的是this_thread::sleep( 0 ); 是否永远沉睡......

标签: c++ multithreading boost time sleep


【解决方案1】:

boost::this_thread::sleep(0) 不应该“暂停线程”;它应该立即返回。已经发布了关于 boost::this_thread::sleep 挂起的错误报告,所以可能是您遇到了这个错误 --- 如果是这样,我将不胜感激,因为我自己无法重现它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多