Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does threadpool get shared between application domains?

Consider a process which is creating multiple application domains. Do these Application domains share same thread pool? If yes, how is it coordinated between multiple application domains?

like image 961
TAdhav Avatar asked Aug 10 '10 09:08

TAdhav


People also ask

When should you not use ThreadPool?

Thread pools do not make sense when you need thread which perform entirely dissimilar and unrelated actions, which cannot be considered "jobs"; e.g., One thread for GUI event handling, another for backend processing. Thread pools also don't make sense when processing forms a pipeline.

Is ThreadPool a process?

Once a thread in the thread pool completes its task, it's returned to a queue of waiting threads. From this moment it can be reused. This reuse enables applications to avoid the cost of creating a new thread for each task. There is only one thread pool per process.

What is an ThreadPool in C#?

Thread pool in C# is a collection of threads. It is used to perform tasks in the background. When a thread completes a task, it is sent to the queue wherein all the waiting threads are present. This is done so that it can be reused.

How many threads does a thread pool have?

ThreadPool will create maximum of 10 threads to process 10 requests at a time. After process completion of any single Thread, ThreadPool will internally allocate the 11th request to this Thread and will keep on doing the same to all the remaining requests.


2 Answers

The ThreadPool is shared across all appdomains - since that means threads might end up switching between appdomains (potentially often!) there's been perf work around that:

http://blogs.msdn.com/b/ericeil/archive/2009/04/23/clr-4-0-threadpool-improvements-part-1.aspx

[...] In fact, we violate this “rule” already: since .NET 3.5, the CLR thread pool has maintained separate FIFO queues for each AppDomain in the process, and an additional independent FIFO queue for “native” work items such as those queued by a host (ASP.net being the prime user of this feature). We round-robin between these work queues, allowing each to execute work for some time before moving on to the next.[...]

BTW, note that strictly speaking the ThreadPool isn't shared across the entire process anymore, since the v4 CLR allows loading side-by-side with V2, and each will have its own threadpool.

like image 51
James Manning Avatar answered Sep 27 '22 18:09

James Manning


The threadpool is shared between all appdomains, as each threadpool thread is context-agnostic and the whole threadpool runtime profile is highly dependent on the hardware you are running on (# of procs, hyperthreading and such)

There is one thread pool per process. The thread pool has a default size of 25 threads per available processor. The number of threads in the thread pool can be changed using the SetMaxThreads method. Each thread uses the default stack size and runs at the default priority.

Source : http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx

If I remember correctly, the CLR handles the threadpool threads internally and cleans the thread context before serving another work request.

like image 33
Florian Doyon Avatar answered Sep 27 '22 18:09

Florian Doyon