考虑以下示例,在 windows 7 icore7 笔记本电脑(VC++2010)和 ubuntu 64bit 12.04 lte gcc 4.6.3
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/thread.hpp>
typedef boost::posix_time::ptime Time;
typedef boost::posix_time::time_duration TimeDuration;
int main()
{
Time t1;
Time t2;
TimeDuration dt;
boost::posix_time::microseconds tosleep=boost::posix_time::microseconds(100);
for(int i=0;i<10;i++){
t1=boost::posix_time::microsec_clock::local_time();
//std::cout <<i << std::endl; // on win7 without this all outputs are 0
boost::this_thread::sleep( tosleep );
t2=boost::posix_time::microsec_clock::local_time();
dt = t2 - t1;
long long msec = dt.total_microseconds();
std::cout << msec << std::endl;
}
return 0;
}
我原以为我的线程会持续休眠 100 微秒,但输出有点奇怪:
=========== AND OUTPUT ==================
arm2arm@cudastartub:~$ g++ -O3 sleepme.cpp -lboost_thread
arm2arm@cudastartub:~$ ./a.out
726
346
312
311
513
327
394
311
306
445
boost有一些开销吗?对于微秒很重要的实时系统,我需要什么?