I have a HANDLE to the waitable timer that can be shared among many running threads in my Windows service for the APIs such as CreateWaitableTimer, WaitForSingleObject, SetWaitableTimer and CancelWaitableTimer.
My question is, do I need to synchronize access (read/write) to this HANDLE itself via a mutex or a critical section?
EDIT: OK, I guess I need to clarify (with code):
//Say, I have a global handle
HANDLE ghWaitableTimer = NULL; //Originally set to NULL
...
//Thread one
if(!ghWaitableTimer)
{
ghWaitableTimer = ::CreateWaitableTimer(NULL, FALSE, NULL);
}
...
//Thread two
if(ghWaitableTimer)
{
::SetWaitableTimer(ghWaitableTimer, &liWhen, 0, NULL, NULL, FALSE);
}
...
//Thread three
if(ghWaitableTimer)
{
::WaitForSingleObject(ghWaitableTimer, INFINITE);
}
Or do I need to do this:
//Say, I have a global handle
HANDLE ghWaitableTimer = NULL; //Originally set to NULL
CRITICAL_SECTION CriticalSection; //Must be initialized before it's used
...
//Thread one
::EnterCriticalSection(&CriticalSection);
if(!ghWaitableTimer)
{
ghWaitableTimer = ::CreateWaitableTimer(NULL, FALSE, NULL);
}
::LeaveCriticalSection(&CriticalSection);
...
//Thread two
::EnterCriticalSection(&CriticalSection);
HANDLE hTimer = ghWaitableTimer;
::LeaveCriticalSection(&CriticalSection);
if(hTimer)
{
::SetWaitableTimer(hTimer, &liWhen, 0, NULL, NULL, FALSE);
}
...
//Thread three
::EnterCriticalSection(&CriticalSection);
HANDLE hTimer = ghWaitableTimer;
::LeaveCriticalSection(&CriticalSection);
if(hTimer)
{
::WaitForSingleObject(hTimer, INFINITE);
}