where/how can I find whether a .net class uses IOCP?

前端 未结 2 508
孤城傲影
孤城傲影 2020-12-29 06:04

Update

I asked the wrong question, rephrased (based on the great info on answers and comments):

Is there any good source on .net\'s as

2条回答
  •  无人及你
    2020-12-29 06:13

    Generally, the BCL only offers async APIs if they are implemented using async IO backed by the Windows kernel. Exposing async methods that do not use async kernel-IO would be the well-known async-over-sync anti-pattern which the BCL designers are surely aware of. This would not only be useless, but harmful for performance and misleading. They don't do that.

    Windows can do async IO using IOCP or using regular overlapped IO. Both are efficient, asynchronous and therefore more scalable than blocking IO.

    All of this is transparent to you. Rely on async being truly async, and sync being truly sync.

    If in doubt, peek under the hood with Reflector. Whenever I have done this I have found confirmed what I just stated. I have yet to see a deviating case.

    What you see with Reflector is that the BCL is calling the async versions of the relevant Win32 APIs. As an example, I'll examine files and sockets:

    • FileStream.BeginRead indirectly calls Win32Native.ReadFileNative with a pointer to a NativeOverlapped structure. The pointer is obtained by calling Overlapped.Pack. The completion callback is stored that way. It is impossible to track how the callback is called with Reflector because that part exists in the native part of the CLR. I cannot tell whether IOCP is in use but I can tell that async IO is in use.
    • Socket.BeginRead indirectly calls WSARecv. The code is quite complex. The BCL seems to be able to use overlapped IO as well as IOCP depending on the OS. The check is made in Socket.InitializeSockets. The decision what kind of IO to use is stored in Socket.UseOverlappedIO. If that variable is false, Socket.BindToCompletionPort is eventually called.

    So for Sockets it is clearly IOCP on modern OS'es. For files I cannot tell.

    I personally am not particularly interested in what kind of async IO is used as long as it is non-blocking. This is the case.

提交回复
热议问题