Rainny
2006-11-13 02:53:13 UTC
I use timer queue API to create an event object which is fired at
regular interval say 10 milliseconds. The precision seems not very
satisfying. I wonder if it's natural for a non-realtime OS, and is
there
anything I can do with it? Thanks.
Here's the code:
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <iostream>
static volatile DWORD last;
VOID CALLBACK timer_proc(PVOID, BOOLEAN)
{
DWORD cur = ::GetTickCount();
std::cout << "timer_proc() called, delta = " << cur - last <<
std::endl;
last = cur;
}
int main(int argc, char *argv[])
{
::timeBeginPeriod(1);
last = ::GetTickCount();
// Create timer queue
HANDLE handle;
::CreateTimerQueueTimer(&handle, NULL, timer_proc, NULL, 10, 10, 0);
::Sleep(110);
// Change due time & period
std::cout << "Changing timer, delta = " << ::GetTickCount() - last <<
std::endl;
last = ::GetTickCount();
::ChangeTimerQueueTimer(NULL, handle, 130, 20);
::Sleep(310);
// Terminate timer
std::cout << "Deleting timer, delta = " << ::GetTickCount() - last <<
std::endl;
last = ::GetTickCount();
::DeleteTimerQueueTimer(NULL, handle, INVALID_HANDLE_VALUE);
std::cout << "Timer deleted, delta = " << ::GetTickCount() - last <<
std::endl;
::timeEndPeriod(1);
return 0;
}
Compile with 'cl timer_queue_test.cpp /EHsc winmm.lib' under Windows XP
Pro sp2, Visual C++ 2005, AMD A64 2800 OC 3200.
The output is like this:
X:\Test>timer_queue_test.exe
timer_proc() called, delta = 16
timer_proc() called, delta = 15
timer_proc() called, delta = 16
timer_proc() called, delta = 0
timer_proc() called, delta = 16
timer_proc() called, delta = 15
timer_proc() called, delta = 0
timer_proc() called, delta = 16
timer_proc() called, delta = 15
timer_proc() called, delta = 0
Changing timer, delta = 16
timer_proc() called, delta = 125
timer_proc() called, delta = 16
timer_proc() called, delta = 31
timer_proc() called, delta = 16
timer_proc() called, delta = 15
timer_proc() called, delta = 16
timer_proc() called, delta = 31
timer_proc() called, delta = 16
timer_proc() called, delta = 15
Deleting timer, delta = 16
Timer deleted, delta = 0
regular interval say 10 milliseconds. The precision seems not very
satisfying. I wonder if it's natural for a non-realtime OS, and is
there
anything I can do with it? Thanks.
Here's the code:
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <iostream>
static volatile DWORD last;
VOID CALLBACK timer_proc(PVOID, BOOLEAN)
{
DWORD cur = ::GetTickCount();
std::cout << "timer_proc() called, delta = " << cur - last <<
std::endl;
last = cur;
}
int main(int argc, char *argv[])
{
::timeBeginPeriod(1);
last = ::GetTickCount();
// Create timer queue
HANDLE handle;
::CreateTimerQueueTimer(&handle, NULL, timer_proc, NULL, 10, 10, 0);
::Sleep(110);
// Change due time & period
std::cout << "Changing timer, delta = " << ::GetTickCount() - last <<
std::endl;
last = ::GetTickCount();
::ChangeTimerQueueTimer(NULL, handle, 130, 20);
::Sleep(310);
// Terminate timer
std::cout << "Deleting timer, delta = " << ::GetTickCount() - last <<
std::endl;
last = ::GetTickCount();
::DeleteTimerQueueTimer(NULL, handle, INVALID_HANDLE_VALUE);
std::cout << "Timer deleted, delta = " << ::GetTickCount() - last <<
std::endl;
::timeEndPeriod(1);
return 0;
}
Compile with 'cl timer_queue_test.cpp /EHsc winmm.lib' under Windows XP
Pro sp2, Visual C++ 2005, AMD A64 2800 OC 3200.
The output is like this:
X:\Test>timer_queue_test.exe
timer_proc() called, delta = 16
timer_proc() called, delta = 15
timer_proc() called, delta = 16
timer_proc() called, delta = 0
timer_proc() called, delta = 16
timer_proc() called, delta = 15
timer_proc() called, delta = 0
timer_proc() called, delta = 16
timer_proc() called, delta = 15
timer_proc() called, delta = 0
Changing timer, delta = 16
timer_proc() called, delta = 125
timer_proc() called, delta = 16
timer_proc() called, delta = 31
timer_proc() called, delta = 16
timer_proc() called, delta = 15
timer_proc() called, delta = 16
timer_proc() called, delta = 31
timer_proc() called, delta = 16
timer_proc() called, delta = 15
Deleting timer, delta = 16
Timer deleted, delta = 0