【问题标题】:boost c++ libraries: sleep_for wait (almost) twiceboost c++ 库:sleep_for 等待(几乎)两次
【发布时间】:2013-05-22 13:59:45
【问题描述】:

我正在使用 Boost C++ 库,但我在使用 `sleep_for` 函数时遇到了一些问题:它需要很长时间!!!如果我只想让一个线程休眠 1 秒,它会让线程休眠 2 秒!!!

我在 Windows 7 (x64) 上使用 msvc 2010 作为编译器。这些库是在 x86 指令集中编译的。 我的应用程序有大约 5 个线程,我不想用 Sleep 调用替换(如果可能的话),因为我正在使用中断点。

我有一些替代品吗?一世

一些例子:

static const boost::chrono::microseconds timeToSleep( 40000 );

DWORD dwStart = timeGetTime();
boost::this_thread::sleep_for( timeToSleep );
DWORD elapsed = timeGetTime() - dwStart;
BOOST_LOG_TRIVIAL( trace ) << "elapsed: " << elapsed << "ms" << std::endl;

这个打印总是80双

如果我写:

static const boost::chrono::microseconds timeToSleep( 80000 );

简而言之,如果我想要 sleep x,它会睡 2x。我已经在调试和发布模式之间切换,但结果相同。
我错了吗???这很奇怪

编辑

#define BOOST_TEST_MODULE SLEEP_FOR_TEST_SUIT
#include <boost/test/unit_test.hpp>
#include <boost/log/trivial.hpp>
#include <boost/thread.hpp>

#include <Windows.h>
#include <MMSystem.h>
#pragma comment( lib, "Winmm.lib" )

using namespace boost;


BOOST_AUTO_TEST_CASE( sleep_test )
{
    for ( int i = 0; i < 1000000; i += 10000 )
    {
        chrono::microseconds toSleep( i );
        DWORD dwStart = timeGetTime();
        this_thread::sleep_for( toSleep );
        BOOST_LOG_TRIVIAL( info )
            << "this_thread::sleep_for( chrono::microseconds( " << i / 1000 << " ) has slept: "
            << timeGetTime() - dwStart << "ms";
    }
}

这总是睡两次

【问题讨论】:

  • In few word if I want sleep x it sleeps 2x. 为什么不告诉它睡觉x/2
  • @Arthur,这是什么解决方案?
  • @Arthur:如果不了解 2 的神秘因子从何而来,就无法知道对输入进行转换是否会可靠地提供所需的输出。
  • 我会用 sleep_for 测试写一个简单的单线程应用程序

标签: c++ boost boost-thread


【解决方案1】:

如果你使用 boost 1.53,那么它就是boost bug。解决方案是修补 boost 源、等待新版本或回滚到以前的 boost 版本。

【讨论】:

  • 在 boost 上看到这种错误很奇怪。似乎他们没有测试他们的库!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-26
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多