Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's difference between windows thread pool and CLR thread pool

I read some segments about window thread pool. It looks like CLR thread pool.

CLR is based on Windows, so CLR thread is based on windows thread pool, is it right?

I know that each .net process has one thread pool, what's the situation in windows thread pool? The OS has one thread pool or many?

In C#, can developer control the window thread pool by code?

like image 591
roast_soul Avatar asked Jul 12 '14 12:07

roast_soul


1 Answers

It is one of those CLR implementation questions that doesn't have a straight answer. It is not up to the CLR to determine how the ThreadPool is implemented. It is the job of the CLR host. A layer of software that integrates the CLR with the operating system. The core interface that the CLR uses to get thread-pooly things done is IHostThreadPoolManager. It is an unmanaged COM interface but you'll have little trouble recognizing the almost one-to-one mapping with ThreadPool class members.

There are many implementations of the CLR host. The more recognizable ones are the default CLR host for desktop apps, implemented by mscoree.dll. There are different versions of it for different Windows versions. And ASP.NET, Sql Server, the Visual Studio Hosting process, the custom host for Silverlight, Windows Phone, XBox. And the less recognizable ones, large unmanaged apps can host the CLR themselves in order to support scripting implemented in a .NET language. CAD programs like AutoCAD etc are standard examples.

The core notion of a thread is virtualized in the CLR. IClrTask and IClrTaskManager are the hosting interfaces for that. Which allows a host to implement a thread on something else than an operating system thread. Like a fiber. Nobody actually does this btw.

Sure, Windows has its own api for a threadpool. The CreateThreadPool() winapi function gets that ball rolling. However, poking around the mscor*.dll files on my machine with dumpbin.exe /imports, I do not see it being used. At least part of the problem might be that CreateThreadPool() is a later winapi function, available only since Vista. XP and earlier Windows versions had a much simpler implementation. So, no, at least for the desktop version of .NET 4.5.2, the Windows threadpool does not appear to be relevant.

like image 193
Hans Passant Avatar answered Oct 19 '22 10:10

Hans Passant