Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Deleting an event while WaitForSingleObject?

696 views
Skip to first unread message

christ2008

unread,
Jan 18, 2008, 11:35:00 AM1/18/08
to
What happens if I call CloseHandle() for an hEvent while that event is
being waited on using WaitForSingleObject() ?

Is the behavior in Win32 defined for such case?

Will this result in that WaitForSingleObject() entering a deadlock?

Or is the system smart enough to let WaitForSingleObject() with some
kind of diagnostic/error code?

Thanks,
Chris

Scott McPhillips [MVP]

unread,
Jan 18, 2008, 1:05:17 PM1/18/08
to
"christ2008" <christ...@mailinator.com> wrote in message
news:a851791d-1c8b-4ce3...@f47g2000hsd.googlegroups.com...

If you really want to know then you'll have to try it and see. The result
is not defined by Win32, and doing this would be considered a design error.

--
Scott McPhillips [VC++ MVP]

christ2008

unread,
Jan 18, 2008, 2:48:46 PM1/18/08
to
On Jan 18, 1:05 pm, "Scott McPhillips [MVP]" <org-dot-mvps-at-

scottmcp> wrote:
>
> If you really want to know then you'll have to try it and see.  The result
> is not defined by Win32, and doing this would be considered a design
> error.

Thanks, Scott. Your reply prompted me to look again at the
documentation for WaitForSingleObject() and sure enough it clearly
says that the behavior for such case is undefined. I somehow
overlooked that sentence.

So does this mean that if I initialize a subsystem in which there is a
WaitForSingleObject(), there is no way of "un-initializing" it? The
only way out is killing the process (exiting the program)?

Thanks,
Chris

Norman Bullen

unread,
Jan 18, 2008, 8:00:55 PM1/18/08
to
What kind of event is it on which you are waiting?

Can you arrange for the event to become set so that the wait ends normally?

Norm

--
Norm

To reply, change domain to an adult feline.

Jeffrey Adler

unread,
Jan 19, 2008, 12:21:43 PM1/19/08
to
If you want to do something like that, create a 2nd event object, do a
WaitForMultipleObjects, and if the 2nd object gets signalled it can close
the first object normally.


"Norman Bullen" <no...@BlackKittenAssociates.com> wrote in message
news:13p2ibb...@corp.supernews.com...

Ulrich Eckhardt

unread,
Jan 21, 2008, 3:58:59 AM1/21/08
to
christ2008 wrote:
> [...] Your reply prompted me to look again at the

> documentation for WaitForSingleObject() and sure enough it clearly
> says that the behavior for such case is undefined. I somehow
> overlooked that sentence.
>
> So does this mean that if I initialize a subsystem in which there is a
> WaitForSingleObject(), there is no way of "un-initializing" it?

I'm not exactly sure what you are trying to do, but this should work:

h = DuplicateHandle(h);
WaitFor..(h);
CloseHandle(h);

That way, even calling CloseHandle() from a different thread doesn't destroy
the event. I'm not 100% sure if event handles can be duplicated though, but
the returnvalue and documentation of DuplicateHandle() will tell you. ;)

Other than that, you can use two events, one for the actual task, the other
to signal the thread to terminate.

Uli


--
Sator Laser GmbH
Geschäftsführer: Michael Wöhrmann, Amtsgericht Hamburg HR B62 932

0 new messages