I'm using Windows keyed events for custom synchronization primitives, inspired by http://locklessinc.com/articles/keyed_events/
To support thread interruption, I've changed places were blocking calls like Sleep or WaitForSingleObject were used to their alertable versions like SleepEx and WaitForSingleObjectEx. Then I can interrupt a wait by queueing an APC with QueueUserAPC and if the returns is WAIT_IO_COMPLETION I throw a special exception which causes all the destructors to be called and then the thread exit. This is similar to the Linux version of pthread_cancel() which causes the stack to unwind and so stuff is cleaned up when the thread exits. I see that NtWaitForKeyedEvent() has an Alertable parameter as well, so I'm wondering if I can do the same thing for this case, or there are caveats? If开发者_Go百科 an APC would wake this up, I also don't know what return value to look for, since NTSTATUS does not directly correspond to WIN32 return codes. There is a STATUS_USER_APC and a STATUS_ALERTED and I don't know if it would be either of those two. If a user APC function is not sufficient to alert the keyed event wait, what is?NtWaitForKeyedEvent
with Altertable = TRUE does look like it will do what you want. That's the parameter you set with NtWaitForSingleObject
to enable reciept of APCs as well.
You will recieve STATUS_USER_APC
if the wait completed due to an APC being run and STATUS_ALERTED
if NtAlertThread
is called in connection with your thread. If all you want is a signal to stop the wait, NtAlertThread
is sufficient.
If your exception handling mechanism does SEH unwinds you can probably throw the special exception directly from the APC routine.
精彩评论