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)?