小编buc*_*tak的帖子

C++提高睡眠准确性

我遇到了boost::sleep()功能上的奇怪问题.我有这个基本代码:

#include <sys/time.h>
#include <boost/chrono.hpp>
#include <boost/thread.hpp>

void thread_func()
{
    timeval start, end;
    gettimeofday( &start, NULL );
    boost::this_thread::sleep( boost::posix_time::milliseconds(1) ); // usleep(1000) here works just fine.
    gettimeofday( &end, NULL );

    int secs = end.tv_sec - start.tv_sec;
    int usec = end.tv_usec - start.tv_usec;
    std::cout << "Elapsed time: " << secs << " s and " << usec << " us" << std::endl;
}

int main()
{
    thread_func();

    boost::thread thread = boost::thread( thread_func );
    thread.join();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

问题是boost::sleep() …

c++ sleep boost-thread

3
推荐指数
1
解决办法
2582
查看次数

标签 统计

boost-thread ×1

c++ ×1

sleep ×1