Search code examples
windowsasynchronousoverlapped-io

Windows overlapped IO vs IO on separate thread


On windows, when you do I/O, you can do it async using the OVERLAPPED option. Is there any difference between doing that vs performing the I/O synchronously on another thread? If so, which is better? Does the OS just spawn a separate thread in async case, or does it just queue it on the driver thread and send signal instead of block wait?

Thanks!


Solution

  • Windows I/O is inherently asynchronous, so performing an async operation in .NET for example should not use a thread, once the operation completes some existing threads are briefly borrowed to notify of the operation's completion, but no threads are created.

    That's quite different from running a synchronous operation on another thread. It uses up a thread which makes the program much less scalleable. In .NET a thread has a default local storage of 1MB so having thousands of threads running will consume gigabytes of memory. Then you also have the additional cost of switching between threads, which is usually small but can add up if you have a lot of threads.