0

I stumbled across a problem which invokes some time critical functionalities. My thread has to perform some actions and then sleep for exactly 60ms. Since required platform is Windows (language C++) and I know it really has a problems with time measurements like this I got an idea to increase accuracy of thread sleep.

Instead of:

void run() {
    do_some_action();
    sleep(60); // ms
}

I wanted to make it like this:

void run() {
    do_some_action();
    curr_time = get_time_in_nanosec(); // with QueryPerformanceFrequency/QueryPerformanceCounter
    while(get_time_in_nanosec() - curr_time < 60000000 /*60ms*/) {}
}

Is it worth additional CPU usage and how could I measure performance (is it sleeping correctly)?

carobnodrvo
  • 1,021
  • 1
  • 9
  • 32
  • What are you trying to do exactly? There may be better solutions if we have more information. – const_ref Apr 19 '17 at 12:25
  • @const_ref Just need a thread to sleep for 60ms (as precisely as possible). – carobnodrvo Apr 19 '17 at 12:27
  • 1
    Possible duplicate of [Precise thread sleep needed. Max 1ms error](http://stackoverflow.com/questions/13397571/precise-thread-sleep-needed-max-1ms-error) – Beginner Apr 19 '17 at 12:31
  • This is "not nice". What if I also want *my* program to have an exact wait and add another loop? What happens when all cores are taken? – Bo Persson Apr 19 '17 at 12:52
  • The only way to be sure it to not use Windows, (or Linux, or any other desktop OS). You need dedicated hardware. – ThingyWotsit Apr 19 '17 at 15:30
  • 2
    Also 'exactly 60ms' is not realisable in any scenarion. You can get really close with, say a hydrogen fountain maser clock, but it's a bit expensive. You need to supply limits, eg. '60ms +- 1ms' – ThingyWotsit Apr 19 '17 at 15:33
  • what exactly does _"**exactly** 60ms"_ mean? Can you provide tolerances? – Arno Apr 19 '17 at 16:56

0 Answers0