July 13th, 2016

What happens if I have multiple asynchronous ReadDirectoryChangesW calls outstanding on the same directory handle?

A customer ran into an issue with the Read­Directory­ChangesW function. It was rather complicated, but one corner of the issue boiled down to the following: The customer had multiple asynchronous calls to Read­Directory­ChangesW outstanding. What if anything can be said about the results?

First, we’ll give the answer while wearing kernel-colored glasses: The kernel completes the calls to Read­Directory­ChangesW in the order they were issued, so that the earliest call receives the first available batch of changes, and the next call receives the next available batch of changes, and so on.

Now let’s take off our kernel-colored glasses and see what this means for the application.

Even though the kernel completes the calls in the order they were issued, the fact that the scheduler can preempt a thread at any time means that even though the two operations complete in sequence, the two threads that handle the completion are at the mercy of the scheduler, and they will race against each other, so the end result is unpredictable anyway.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.