Is IOCP a thread that runs during I / O or after?

I am trying to understand the I / O completion ports and, in particular, how they relate to use async- awaitfor I / O.

The infamous No Thread article suggests that IOCPs are borrowed shortly after I / O completes. Since the whole point of the article is to show that when the fantastic hardware I / O level is in flight, there is no thread that is consumed by the loop, for example

Is I / O ready too? No. Is an I / O operation in progress? No. Is an I / O operation in progress? No.

But then I look at this article , which says that

"responsible for checking the port of completion for the queue elements"

and gives an example of type

public class IOCompletionWorker
{ 
    public unsafe void Start(IntPtr completionPort)
    {
        while (true)
        {
            uint bytesRead;
            uint completionKey;
            NativeOverlapped* nativeOverlapped;

            var result = Interop.GetQueuedCompletionStatus(
                completionPort, 
                out bytesRead,
                out completionKey,
                &nativeOverlapped, 
                uint.MaxValue);

            var overlapped = Overlapped.Unpack(nativeOverlapped);

            if (result)
            {
                var asyncResult = ((FileReadAsyncResult)overlapped.AsyncResult);
                asyncResult.ReadCallback(bytesRead, asyncResult.Buffer);
            }
            else
            {
                ThreadLogger.Log(Interop.GetLastError().ToString());
            }

            Overlapped.Free(nativeOverlapped);
        }
    }
}

var completionPortThread = new Thread(() => new IOCompletionWorker().Start(completionPortHandle))
{
    IsBackground = true
};
completionPortThread.Start();

which seems to me that some kind of poll is taking place.

I think my questions come down to

  • Is it true that a .NET application has 2 types of thread pools - (1) "worker threads" and (2) "input / output streams"?
  • If true, is there a fixed number specified in the configuration, for example, M workflows and N I / O threads? And, as a rule, the ratio of M to N?
  • When are I / O streams used?
+6
source share
1 answer

Both articles are true in their own way.

IOCP - . , ​​( PostQueuedCompletionStatus) . , IOCP, -consummer.

, :

  • WSARecv , IOCP, , , . , .
  • . . . , , IOCP, IOCP. .

( ) . , (, , , !), IOCP.

IOCP - , IO- (, ,...) IOCP. .

, , GetQueuedCompletionStatus, , IOCP , , , . IOCP , , , . 1-to-1-to-1 IO-/IOCP/ Thread, , IO.

IOCP parallelism Windows.

, .

  • , .Net . , "Worker" threadpool. - "IO". , IOCP # .
  • , , , . , , , IO. IO, , - , - . ThreadPool.SetMinThreads/SetMaxThreads, , , , threadpool.
  • IO , , IOCP . , -. UnsafeQueueNativeOverlapped, .

(, async- Task.Delay, ) -, IOCP , "".

- -. "ThreadPoolWorkQueue.Dispatch", - "_IOCompletionCallback.PerformIOCompletionCallback". , , , .

+5

All Articles