I am working towards achieving +/- ms timing precision. I want the time between threads to be 10ms, but when I measure the time I get something closer to 15ms.
I am trying to understand if the problem is due to the way I am measuring time or if I am measuring the time accurately and there is some delay introduced by CreateTimerQueueTimer
My code looks like
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <chrono>
#include <ctime>
using namespace std;
int current;
long long* toFill;
void talk()
{
chrono::time_point<chrono::system_clock> tp = \
chrono::system_clock::now();
toFill[current++]=chrono::duration_cast<chrono::milliseconds>(tp.time_since_epoch()).count() ;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hTimer;
current = 0;
toFill = new long long[1000];
CreateTimerQueueTimer(&hTimer,NULL,
(WAITORTIMERCALLBACK)talk,NULL,
0,10,0);
_sleep(3000);
DeleteTimerQueueTimer(NULL,hTimer,NULL);
for (int i = 1;i<current;i++)
{
cout << i << " : " << toFill[i]-toFill[i-1]<< endl;
}
return 0;
}
Output looks like
...
161 : 16 <-- Should be 10
162 : 15
163 : 16
164 : 16
...
The accuracy of timers and the real time clock in Windows is limited by the clock tick interrupt rate. Which by default tocks 64 times per second, 1/64 sec = 15.625 msec. Just like you saw.
Increasing that rate is actually possible, call timeBeginPeriod(10) at the start of your program to get 10 msec accuracy.