2012-06-13 4 views
0

창에서 예를 들어 다음과 같은 7 icore7 노트북 (++ 2010 VC)을 고려하고 우분투는 64 비트 12.04 LTE의 GCC 내 스레드가 지속적으로 100 마이크로 잠을하지만, 출력은 뭔가 이상한 것을 예상하고 있었다 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; 
} 

:

=========== AND OUTPUT ================== 
[email protected]:~$ g++ -O3 sleepme.cpp -lboost_thread 
[email protected]:~$ ./a.out 
726 
346 
312 
311 
513 
327 
394 
311 
306 
445 

부스트가 약간의 오버 헤드가 있습니까? 마이크로 초가 중요한 RealTime 시스템에 필요한 것은 무엇입니까?

답변

0

Windows에서는 수면 (tosleep) 호출이 절전 (0)과 동일하므로 1ms 미만의 절전 (1 초 미만)을 수행 할 수 없습니다. (this link 참조)

물론 std::cout <<i << std::endl은 시간이 좀 걸릴 것입니다 ...