我遇到了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() …