问题描述
在Windows中使用具有重叠IO的文件写入有什么优势吗?
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?
[编辑 - 请注意,我正在做文件写入而不使用系统缓存,即我在CreateFile中使用FILE_FLAG_NO_BUFFERING标志)
[Edit - please note that I'm doing the file writes without system caching, ie I use the FILE_FLAG_NO_BUFFERING flag in CreateFile)
推荐答案
默认情况下缓存在系统缓存中,做重叠I / O或创建一个单独的线程用于写入没有什么优势。大多数WriteFile调用只是在它们的核心的memcpys,它们被OS以最佳方式用其他写入方式写入磁盘。
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.
当然,您可以关闭缓冲I / O通过标志到CreateFile,然后有一些优点,做一些异步I / O - 但你可能没有/不应该这样做。
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.
修改
OP已澄清他们实际上使用的是未缓冲的I / O。在这种情况下,两个建议的解决方案几乎相同;内部Windows使用线程池来服务异步I / O请求。但是假设Windows可以更高效,因为它们的一半在内核中实现,具有较少的上下文切换等。
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.
这篇关于使用重叠IO进行文件写入与在单独的线程中写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!