hadi teo
hadi teo

Reputation: 936

Task Parallel Library : How many thread does the application spawn?

We are now using Task Parallel Library by implementing Task.Factory.StartNew(). Is there any way to check how many threads does the application spawn when executing the task ?

Currently we are running the application in dual core processor in the development environment.

Upvotes: 0

Views: 1071

Answers (1)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131364

TPL doesn't spawn any threads when executing a task unless you use a custom scheduler or you pass the TaskCreationOptions.LongRunning option. Even then, it is up to the TaskScheduler used to decide how to treat long-running tasks.

TPL schedules individual tasks to a threadpool for execution by the pool's threads. Each Thread has its own queue to reduce conflicts in multi-core machines. If a thread is too busy, the Framework uses some work-stealing magic to assign the task to an idle thread in the same thread pool.

Check How does the tpl use the CLR thread pool for a bit more info, and this post by Daniel Moth for details on work stealing.

Upvotes: 3

Related Questions