Re: Timer with Micro seconds



In Chandra's code there is design flaw. Under some conditions the expression

liTimeOut.QuadPart += liDelay.QuadPart

can result in small value, when the addition overflows. The better way is to
calculate the difference between current time and start time and compare it
with the timeout.

I must admit that the probability for the overflow is small, but it's
different from zero.

LARGE_INTEGER liDelay;
// Query number of ticks per second
if (QueryPerformanceFrequency(&liDelay))
{
// 1ms delay
liDelay.QuadPart /= 1000;

LARGE_INTEGER liTimeOut;
// Get current ticks
if (QueryPerformanceCounter(&liTimeOut))
{
// Create timeout value
liTimeOut.QuadPart += liDelay.QuadPart;
LARGE_INTEGER liCurrent;
do
{
// Get current ticks
QueryPerformanceCounter(&liCurrent);
// Delay until timeout
} while (liCurrent.QuadPart<liTimeOut.QuadPart);
}
}


LARGE_INTEGER liStart;
// Get current ticks
if (QueryPerformanceCounter(&liStart))
{
do
{
LARGE_INTEGER liCurrent;
LARGE_INTEGER liSpan;

// Get current ticks
QueryPerformanceCounter(&liCurrent);
liSpan.QuadPart = liCurrent.QuadPart - liStart.QuadPart;
} while (liSpan.QuadPart<liTimeOut.QuadPart);
}

/Helge


.



Relevant Pages

  • Updating callout_reset
    ... #1 - At high hz rates, the maximum timeout possible is reduced; ... #2 - Using ticks reduces the potential accuracy of wakeups with our ... hz=100, the only choices is to request one tick, or 10ms. ... precise wakeups in places where the next timer interval was between 5 and ...
    (freebsd-arch)
  • Re: i8042 problem
    ... It looks like a timeout problem. ... but it looks like the timeout is only 10 ticks. ... I'd suggest that we don't even ask the keyboard what scancode set it is ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • Re: Timer with Micro seconds
    ... but my application is hanging when i try to use it. ... // Query number of ticks per second ... // Create timeout value ... // Delay until timeout ...
    (microsoft.public.windowsce.platbuilder)