I have a loop and I want to ensure that it runs for an (approximately) fixed amount of time for each loop.
I am using sleep_for to achieve this behavior but I also want the program to be able to compile on environments that do not include full support of the standard thread library. Right now I have something like this:
using namespace std;
using namespace std::chrono;
//
while( !quit )
{
steady_clock::time_point then = steady_clock::now();
//...do loop stuff
steady_clock::time_point now = steady_clock::now();
#ifdef NOTHREADS
// version for systems without thread support
while( duration_cast< microseconds >( now - then ).count() < 10000 )
{
now = steady_clock::now();
}
#else
this_thread::sleep_for( microseconds{ 10000 - duration_cast<microseconds>( now - then ).count() } );
#endif
}
While this allows the program to compile in environments that do not support standard threads, it is also very CPU-intensive as the program checks continually for the time condition rather than waiting until it is true.
My question is: Is there a less resource-intensive way to enable this "wait" behavior using only standard C++ (i.e. not boost) in an environment that does not fully support threads?
sleep_until. - chrisselecton an empty fdset - n. 1.8e9-where's-my-share m.yieldas well, if supported. - Mahmoud Al-Qudsi