I use timer queues in my application and pass a pointer to one of my own C ++ Timer objects as a parameter for the callback (in CreateTimerQueueTimer). Then I call the virtual method on the object in the callback.
The Timer object's destructor will disable the timer using DeleteTimerQueueTimer ().
static void callback( PVOID param, BOOLEAN timerOrWaitFired )
{
Timer* timer = reinterpret_cast< Timer* >( param );
timer->TimedOut();
}
class Timer
{
public:
Timer();
virtual ~Timer()
{
::DeleteTimerQueueTimer( handle );
}
void Start( double period )
{
::CreateTimerQueueTimer( &handle, ..., &callback, this, ... );
}
virtual void TimedOut() = 0;
...
};
However, there is a subtle race condition that if a callback is already called, but the timer object is destroyed before calling TimedOut (), the application crashes because the callback calls the virtual method on a nonexistent object, or even worse, when it is deleted.
I have mutexes for managing multi-threaded calls, but I still have a problem.
? - , .
? ?
, , , Timer ( , ). , , , Timer , ; , .
.