7

Is there any advantage in using file writing with overlapped IO in Windows, vs just doing the file writing in a separate thread that I create?

[Edit - please note that I'm doing the file writes without system caching, ie I use the FILE_FLAG_NO_BUFFERING flag in CreateFile)

3 Answers 3

4

Since all writes are cached in the system cache by default, there is little advantage to doing overlapped I/O or creating a separate thread for writes at all. Most WriteFile calls are just memcpys at their core, which are lazily written to disk by the OS in an optimal fashion with other writes.

You can, of course, turn off buffered I/O via flags to CreateFile and then there are advantages to doing some sort of async I/O - but you probably didn't/shouldn't do that.

Edit

The OP has clarified they are in fact using unbuffered I/O. In that case the two suggested solutions are nearly identical; internally Windows uses a thread pool to service async I/O requests. But hypothetically Windows can be more efficient because their half is implemented in the kernel, has less context switches, etc.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I'm already doing unbuffered I/O for the speed advantage - I probably should have mentioned (I'll edit my question regarding this)
Correct me if I'm wrong, but if you write to a network filesystem, writes may be buffered differently, and you risk locking up your thread if using blocking IO.
-1 - Overlapped writes on a multicore environment shows increased performance. You can also combine overlapped write from multiple threads to the same file handle - In order todo so without the overlapped flag you will need locks which will further degrade performance. For multiple parallel IO on the same file in Windows you should always use the overlapped flag and either use multiple threads or completion ports - that's a design choice.
2

One advantage with overlapped I/O is that it lets a single thread (or more usually a pool of threads) to handle an arbitrary number of I/O requests concurrently. This might not be an advantage for an single-user, desktop application, but for a server application that can get requests for I/O from many different clients it can be a major win.

Comments

1

Possibly because overlapped I/O in windows will tell Windows to write out the file on it's own time in background, as opposed to spawning a whole new thread and engaging in a blocking operation?

1 Comment

See my answer - Windows already writes out to files on it's own time, in the background, as long as you don't mess with the default parameters and attempt to do something crazy. WriteFileEx and overlapped I/O are not necessary.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.