The documentation for RegisterWaitForSingleObject says
Using a Mutex for waitObject does not provide mutual exclusion for the callbacks because the underlying Win32 API uses the default WT_EXECUTEDEFAULT flag, so each callback is dispatched on a separate thread pool thread. Instead of a Mutex, use a Semaphore with a maximum count of 1.
and (in the documentation for the WaitHandle argument) it says
Use a WaitHandle other than Mutex
This seems to imply that it's safe to use an Event -- is it?
Would there be any difference between using an AutoResetEvent and a ManualResetEvent?开发者_运维知识库
Events are fine. I think I've only ever used events with this method
Re auto vs. manual reset events, the documentation for the underlying Win32 API says that only the object that signalled the end of the wait is affected:
The function modifies the state of some types of synchronization objects. Modification occurs only for the object or objects whose signaled state caused the function to return.
Manual reset events never change state unless you do it, well, manually; I'd expect an auto reset event to reset itself only if it was the one that caused the wait to finish. You would only notice this if you had two auto-reset events signalled simultaneously.
Both AutoResetEvent
and ManualResetEvent
derive from WaitHandle
which is the prescribed use case here. So yes, using both of these classes is safe (if used within the guidelines of the documentation of course).
精彩评论