I have found useful information regarding the use of QueryPerformanceCounter in this post but I am facing an issue for which I have not found an answer.
I am developing an application for Windows CE 6.0 and need a better resolution that GetTickCount can offer. That is why I chose QueryPerformanceCounter function.
I have observed that the counter value I get goes back and forth. Although it is not the final application this little example ilustrates the problem:
int i;
BOOL bRet;
LARGE_INTEGER liCounter;
for ( i = 0; i < 100; i++)
{
bRet = QueryPerformanceCounter(&liCounter);
if(bRet)
{
printf("Counter Value: %llu \n", liCounter.QuadPart);
}
}
It prints a series of 100 counter values which are expected to be incremental. However, there are some counter values which decrement with respect to the previous value. For example:
...
Counter Value: 6536266821
Counter Value: 6536266262
Counter Value: 6536266604
...
This behaviour is problematic since in the final application (endCounterValue-startCoutnerValue) type operations are carried out and negative time intervals are found in same cases.
I have read (here) this problem can be found when using multicore platforms. However, this is not the case since Windows CE 6.0 does not support multicore processing.
Any help to find a reason why does this happen and/or any workaround to avoid this problem would be appreciated.
[Edit]
I edit the question in order to include more information:
A longer list of consecutive reads (different from the list above):
Counter Value: 15234261579
Counter Value: 15234261594
Counter Value: 15234261609
Counter Value: 15234261624
Counter Value: 15234261640
Counter Value: 15234261064
Counter Value: 15234261079
Counter Value: 15234261094
Counter Value: 15234261109
Counter Value: 15234261125
Counter Value: 15234261140
Counter Value: 15234261155
Counter Value: 15234261170
Counter Value: 15234261185
Counter Value: 15234261201
Counter Value: 15234261216
Counter Value: 15234261231
Counter Value: 15234261246
Regarding the hardware, an Intel Celeron 1047UE processor and HM76 chipset are being used.
When calling to QueryPerformanceFrequency, 1.19MHz frequency is read.