On Thu, 1 May 2008 11:14:00 -0700, socatoa
<socatoa@discussions.microsoft.comwrote:
Quote:
>Hi,
>
>I have a DLL in VC6, when a specific function is called it will spawns a
>few threads and then return. The threads stay running and inside one of these
>threads an event is created using the win32 CreateEvent() call:
>
>Code Snippet
>
>static HANDLE hReadyEvent;
>
>.....
>// inside a thread
>hReadyEvent = CreateEvent (NULL, FALSE, FALSE, "DataReady") ;
if (hReadyEvent == NULL)
{
// Error condition
return ;
}
>.....
>// The event is passed to a kernel mode driver, the threads waits for this
>event to be set by the driver. which it does without a problem
retVal = WaitForSingleObject (hReadyEvent, INFINITE);
if (retVal != WAIT_OBJECT_0)
{
//error condition
return ;
}
>// once the thread is finished with the work then closes the handle
>if (hReadyEvent != NULL)
CloseHandle(hReadyEvent);
>
>Once the thread is done with the work, it will terminate itself and the
>other threads. When the specific call from the DLL is called again, then the
>threads will be spawned again, the event will be created again although the
>handle is a static global handle.
>
>This code has been working for many years as is but only when it is been
>running in a multiprocessor system will fail sporadically. The createEvent()
call throws an access violation, but it does not do it every time, it
>occurss very sporadically.
Why does the handle need to be static? If the code you've shown above is
run by only one thread, why not use a local variable? On the other hand, if
multiple threads are waiting on the event, calling CloseHandle when there
are extant waits is undefined. I don't see how that could cause an access
violation the next time you call CreateEvent, but it wouldn't hurt to
check.
--
Doug Harrison
Visual C++ MVP