One of the gotchas of the RegNotifyChangeKeyValue
function is that the notification registration has thread affinity. This is a problem if you want the notification registration to outlive the thread that generated it. In particular, if you register the notification from a nonpersistent thread pool thread, you get into an infinite loop:
- Thread pool task calls
RegNotifyChangeKeyValue
, and waits for the associated event viaRegisterWaitForSingleObject
. - Thread pool thread goes idle.
- Thread pool destroys the idle thread.
- Due to thread affinity, this signals the handle.
- The thread pool queues a task to process the handle that was signaled.
- The task checks the registry key (observes that nothing changed) and calls
RegNotifyChangeKeyValue
again. - Repeat.
Windows 8 added a new flag to the RegNotifyChangeKeyValue
function: REG_NOTIFY_THREAD_AGNOSTIC
. If you pass this flag, then the notification registration does not have thread affinity. If the thread that called RegNotifyChangeKeyValue
exits, the notification registration remains active.
0 comments