Reputation: 2672
The following read claims that when we're in a method A, and we for example read a stream to its end, the read operation will spawn an I/O thread, while the main thread will be waiting on that to complete.
Is that really the case? Why wouldn't the main thread do all that work, instead of waiting on something else to do it? Isn't that the main idea of what "there is no thread" is all about?
Upvotes: 1
Views: 264
Reputation: 1994
Since .NET asynchronous I/O has arisen initially in environment of Windows OS the given considerations are based on the Windows-based I/O concepts.
To start with, there are two types of the I/O flows: synchronous and asynchronous. The former is based on the waiting mechanism which implies that a thread which initiated an I/O operation at some point (typically when request hits a driver) is put into the waiting state by the OS scheduler and is awaken back by the scheduler when the I/O operation completes. The latter is based on specific notification mechanism which implies that after sending an I/O request the thread keeps doing other things and the I/O completion notification is sent separately to this or any other thread depending on the internal threads configuration.
Now as for the I/O threads - the I/O notification mechanism in Windows systems is implemented by using so-called I/O completion ports (IOCP). Briefly an application can create a completion port (you can think of it as a queue) which can be associated with more than one file handle and any thread can be associated to the completion port when it calls specific API on this port for the first time. That way the scheduler keeps the associations between the completion ports and threads which are associated with them to handle I/O completions more efficiently. Briefly a thread which is associated with completion port is put into waiting state and is awaken when the status of completion request is updated. For the .NET world the infrastructure creates the pools of such threads and they are denoted as I/O threads.
The example given in the article implies using synchronous I/O flow with waiting by the initial thread for the I/O operation completion. In contrast an asynchronous I/O scenario from the .NET perspective means using an additional thread for the I/O completion handling (but not earlier than completion occurs).
Upvotes: 2