Question

All:

I am writing a logging solution. One of the available log endpoints is a text file. Suppose I wanted to write to that file from multiple processes: I could open it shared, and use a named mutex to control concurrent access to the file (assuming all access occurred on the same machine). But then I started wondering about async IO. Within a process, I could use BeginWrite to issue my writes asynchronously. What about cross-process, or cross-machine issues? Is async IO safe in those situations?

(assuming that when I call BeginWrite(), the buffer that I pass contains everything that should be kept together in one logical "record")

Was it helpful?

Solution

Before you decide on the async IO solution, note that very often what you think will be an async write ends up being handled synchronously. Particularly important for your logging solution is a little tidbit hidden in Asynchronous Disk I/O Appears as Synchronous on Windows NT, Windows 2000, and Windows XP. Buried there in the discussion is this nugget:

"On Windows NT, any write operation to a file that extends its length will be synchronous."

Although that says "Windows NT," my experience has been that it's true for Windows 2000, Windows XP, and Server 2003 as well. I think they meant "NTFS" rather than "Windows NT". I haven't tested it on Vista or Server 2008.

The article goes on to provide possible solutions to that limitation. I assume they work, but haven't actually tried them.

The most reliable solution I've found is to spawn a thread that does a synchronous write. Kind of a pain in the neck, true, but effective.

OTHER TIPS

I'm not sure what you mean by wrong. If you are guarding access to the file with an appropriate cross process mutex, then even with asynchronous I/O, you will only ever have 1 process writing to the file at a given time. That is presuming you keep the mutex locked until the async I/O completes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top