Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(How) Does TPL use (CLR) Thread Pool?

I am currently researching Task Parallel Library and I read somewhere that TPL actually uses thread pool mechanism from CLR-Level. I couldn't find any article confirming this information. I know, TPL has task queues for each thread and uses some special work-stealing algorithm for balancing. As far as I know, it creates one thread for each processor. Thread pools started to use task objects of TPL since .NET 4.

I can not understand how TPL uses the thread pool. Thread-Pool pattern states, the work items are queued and the free threads in thread pool takes one from this queue. TPL however store the items (tasks) to queues of threads and work-stealing works if needed... Thus, quite differently. Where is my mistake?

extra question: As this was my first Stack Overflow question, I am not sure if it was suitable. Is it?

like image 449
eks Avatar asked Dec 26 '10 17:12

eks


People also ask

Does TPL use ThreadPool?

Thread-Pool pattern states, the work items are queued and the free threads in thread pool takes one from this queue. TPL however store the items (tasks) to queues of threads and work-stealing works if needed...

How is TPL different from thread?

Compared to the classic threading model in . NET, Task Parallel Library minimizes the complexity of using threads and provides an abstraction through a set of APIs that help developers focus more on the application program instead of focusing on how the threads will be provisioned.

What is CLR thread pool?

The CLR thread pool contains two kinds of threads—the worker threads and the I/O completion port or IOCP threads. That means your ASP.Net worker process actually contains two thread pools: the worker thread pool and the IOCP thread pool. Naturally, these pools have different purposes. When you use methods like Task.

How does thread pool works?

A thread pool is a pool threads that can be "reused" to execute tasks, so that each thread may execute more than one task. A thread pool is an alternative to creating a new thread for each task you need to execute.


1 Answers

In TPL, a TaskScheduler is responsible for actually queuing the tasks up for execution. The Default scheduler will use the thread-pool - but I believe it's the new thread pool implementation which actually does the work-stealing cleverness.

Daniel Moth has a blog post with some more details which you may find useful.

like image 147
Jon Skeet Avatar answered Sep 28 '22 00:09

Jon Skeet