A simple description of worker threads and I / O in .NET.

It is very difficult to find a detailed but simple description of worker threads and I / O in .NET.

What is clear to me regarding this topic (but may not be technically accurate):

  • Workflows are threads in which the CPU must use for its work;
  • I / O streams (also called “completion port streams”) should use device drivers to operate and, in essence, “do nothing”, monitor only the completion of operations without a processor.

What is not clear:

  • Although the ThreadPool.GetAvailableThreads method returns the number of available threads of both types, it seems that there is no open API for scheduling work for the I / O thread. Can you manually create a workflow in .NET?
  • It seems that one input / output stream can control several input / output operations. It's true? If so, why does ThreadPool have so many default I / O streams available?
  • In some texts, I read that a callback initiated after an I / O operation completes is performed by an I / O thread. It's true? Isn't that work for the workflow, given that this callback is the work of the CPU?
  • To be more specific, are ASPI running asynchronous user I / O threads? What are the performance benefits when switching I / O to split a thread instead of increasing the maximum number of worker threads? Is it because a single input / output stream keeps track of multiple operations? Or does Windows make context switching more efficient when using I / O streams?
+42
multithreading iocp
Jan 20 '10 at 8:29
source share
4 answers

The term "worker thread" in .net / CLR usually refers only to any thread other than the main thread that does some "work" on behalf of the application that generated the thread. "Work" can really mean anything, including waiting for I / O to complete. ThreadPool stores the workflow cache because threads are expensive to create.

The term “I / O stream” in .net / CLR refers to threads that ThreadPool reserves to send NativeOverlapped callbacks from win32 “bridged” calls (also called “port I / O ports”). The CLR supports its own I / O completion port and can bind any handle to it (via the ThreadPool.BindHandle API). An example here: http://blogs.msdn.com/junfeng/archive/2008/12/01/threadpool-bindhandle.aspx . Many .net APIs use this mechanism to receive NativeOverlapped callbacks, although a typical .net developer will never use it directly.

In fact, there is no technical difference between a "workflow" and an "I / O stream" - they are both just normal threads. But the CLR ThreadPool maintains separate pools for each, to avoid a situation where high demand for workflows exhausts all the threads available for sending its own I / O callbacks, which potentially leads to deadlocks. (Imagine an application using all 250 workflows where everyone is waiting for I / O to complete).

The developer should exercise caution when handling the I / O callback to ensure that the I / O stream is returned to ThreadPool, i.e. the I / O callback code should do the minimum work required to service the callback, and then return flow control to CLR thread pool. If more work is required, this work should be planned on the workflow. Otherwise, the application runs the risk of “hijacking” the CLR pool of reserved I / O streams for use as regular workflows, which will lead to the deadlock situation described above.

Some good links for further reading: win32 I / O Ports: http://msdn.microsoft.com/en-us/library/aa365198(VS.85).aspx managed stream: http://msdn.microsoft.com/ en-us / library / 0ka9477y.aspx BindHandle example: http://blogs.msdn.com/junfeng/archive/2008/12/01/threadpool-bindhandle.aspx

+46
Jan 30 '10 at 17:34
source share

I'll start by describing how asynchronous I / O is used by programs in NT.

You may be familiar with the Win32 ReadFile API function (as an example), which is a wrapper around the Ntree API NtReadFile function . This function allows you to perform two operations with asynchronous I / O:

  • You can create an event object and pass it to an NtReadFile . This event will be reported after the read operation is completed.
  • You can pass an asynchronous procedure call (APC) function to an NtReadFile . Essentially, this means that when a read operation completes, the function will be queued for the thread that initiated the operation, and will be executed when the thread makes an emergency wait.

However, there is a third way to receive notification of an I / O operation. You can create an I / O completion port object and associate files with it. Whenever an operation is performed on a file associated with an I / O completion port, the results of the operation (for example, I / O status) are queued on the I / O completion port. You can then configure a dedicated thread to remove results from the queue and perform related tasks, such as calling callback functions. This is essentially an "I / O workflow."

The usual "workflow" is very similar; instead of removing I / O results from the queue, it removes work items from the queue. You can queue work items ( QueueUserWorkItem ) and execute workflows. This prevents you from creating a thread every time you want to execute a task asynchronously.

+6
Jan 20 '10 at 9:24
source share

Simply put, a workflow is designed to perform a short period of work and will delete itself when it completes it. The callback can be used to notify the parent process that it completed, or to transfer data.

An I / O stream will perform the same operation or series of operations continuously until it is stopped by the parent process. This is called so because usually device drivers constantly monitor the device port. An I / O thread usually generates events whenever it wants to communicate with other threads.

All processes run as threads. Your application works like a thread. Any thread can spawn worker threads or I / O threads (as you call them).

There is always an exact balance between performance and the number or type of threads used. Too many callbacks or events handled by a process greatly degrade its performance due to the number of interruptions in its main process loop when it processes them.

Examples of a workflow would be to add data to the database after interacting with the user or to perform a long mathematical calculation or write the data to a file. Using the workflow, you free the main application, this is most useful for graphical interfaces, since it does not hang during the execution of a task.

+2
Jan 20
source share

Someone with more skills than me will be jumping here to help.

Workflows have many states, they are planned by the processor, etc., and you control everything that they do.

IO Completion Ports are provided by the operating system to perform very specific tasks with a small general state and, therefore, are used faster. A good example in .Net is the WCF environment. Each WCF service “call” is actually made by the I / O completion port, because they start up the fastest and the OS watches them for you.

0
Jan 20 '10 at 9:13
source share



All Articles