There is a win32threadpool under \ clr \ src \ vm \ of the rotor. h and win32threadpool. the cpp file contains "This module implements Threadpool support using Win32 APIs" in the header. It can be seen that the pool is implemented using win32 APIs.
You can see some declarations in the header of the H file:
Static BOOL SetMaxThreads (DWORD MaxWorkerThreads,
DWORD MaxIOCompletionThreads );
Static BOOL GetMaxThreads (DWORD * MaxWorkerThreads,
DWORD * MaxIOCompletionThreads );
Static BOOL SetMinThreads (DWORD MinWorkerThreads,
DWORD MinIOCompletionThreads );
Static BOOL GetMinThreads (DWORD * MinWorkerThreads,
DWORD * MinIOCompletionThreads );
Static BOOL GetAvailableThreads (DWORD * AvailableWorkerThreads,
DWORD * AvailableIOCompletionThreads );
Static BOOL QueueUserWorkItem (LPTHREAD_START_ROUTINE Function,
PVOID Context,
ULONG Flags );
Familiar? Yes, these are the corresponding functions in. NET.
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /////////////////
The text and images in this block are from. NET's ThreadPool Class-Behind The Scenes. You can download The test code, which is very helpful.
Author:Marc Clifton
I Wanted To summarize it myself, but this old brother summed up very well and borrowed it.
QueueUserWorkItem
This method puts the worker thread delegate into a queue and tests whether a new thread should be created. If needed, it will callCreateWorkerThreadMethod and exit. On the contrary, if it is not created at this time, another different thread will call CreateThreadGate to create it if it does not exist.
ShouldGrowWorkerThreadMethod Test three parameters to determine whether a new thread should be created.
It is worth noting that this function first tests whether the number of running threads is smaller than the number of available CPUs. Obviously, this method returns false when one or more running threads run on a single-core machine. At this time, thread gate will be used to create a thread later.
CreateWorkerThreadThe method is used to instantiate an actual working thread.
The worker thread will wait until the WorkRequestNotification event occurs cyclically. If the event times out after 40 s, the event will not be notified. If the event is notified, the Delegate will be deleted from the queue continuously (set the event to the state of not notified), check whether it contains a valid delegate, and then call the delegate. When the delegate returns, the worker thread Immediately checks whether there are additional requests to join the queue. If yes, it will process these requests; if not, it will return the waiting status.
The above code implies that you should do your work as soon as possible. Any event used by your thread will delay processing other requests in the queue.
GateThreadStart
The first method will sleep 0.5 s. Then they will try to determine whether there are some requests in the queue. If not, go back to sleep. If yes, call a method to determine when a new thread is created. The thread creation delay of this method depends on the last thread creation time and the number of running threads.
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////////////////////////
Are there two thread pools?
Both CreateWorkerThread and CreateCompletionPortThread call the CreateUnimpersonatedThread method to create a new thread.
In the end, the CreateThread of WIN32 API is used to create a thread. Therefore, the source of the two threads is the same. Is it possible to calculate the two thread pools logically, it may be because it is more differentiated to respond to the working thread and the IO completion port thread separately. If there are two pools, why not separate the two threads with two sets of APIs and not encapsulate them in a class?
In addition, the working thread differs from the IO completion port thread in that the latter needs to be bound to the IO port and waits until the IO request is executed and returned. (I do not know much about this point. You are welcome to add it ).
It's not too early. Wash and sleep.