/* This class is intended to be used in a loop as a simple method of limiting the loop to * a certain number of iterations per second. Usually, that would be video at 30 frames per * second. * * while( true ) { * rate_limiter lim(0.033); * // grab a frame of video and process it * } */ #include #include #include "rate_limiter.h" rate_limiter::rate_limiter( float secs ) : m_secs(secs) { boost::xtime_get(&xt, boost::TIME_UTC); int sec_part = static_cast(secs); // std::cout << xt.sec << " " << xt.nsec << std::endl; xt.sec += sec_part; xt.nsec += (m_secs - static_cast(sec_part)) * 1000000000.0; // std::cout << secs << " " << sec_part << " " << (m_secs - sec_part) << std::endl; // std::cout << xt.sec << " " << xt.nsec << std::endl; } rate_limiter::~rate_limiter() { boost::thread::sleep(xt); } #ifdef TEST_RATE_LIMITER int main( int argc, char *argv[] ) { if( argc > 1 ) { rate_limiter outer(0.5); // this one should have no effect, since the inner loop should take more than 0.5 seconds for( int x = 30; x > 0; --x ) rate_limiter blah(0.033); } return 0; } #endif