Does a .ForEach loop block when there are no more threads available

353 Views Asked by At

We have a .ForEach loop (TPL) which starts many, many, many Tasks. Since the TPL is consuming threads from the thread pool I am wondering what will happen when there are no more threads available? Will the calling code block until threads are available again?

I know the threadpool has a global work queue where work items (Task) will be queued. Can that queue ever be full?

Our problem is that some of the tasks are long running (30 minutes) and some are short (a second), but we have thousands of such Tasks, if not more. Does the TPL start a new Thread for each Task I start? I think not. At what point will the thread pool be exhausted?

1

There are 1 best solutions below

12
On

When there are no more free Threads there are several algorithms kicking in. Yhe main one is that the ThreadPool will slowly create extra threads (max 2/second).

This helps to address your situation with long-running tasks, but the system is not perfect. Be ware of a situation where hundreds of threads are created, your app will probably crash.

First approach would be to specify a DegreeOfParallelism on the ForEach. You want to limit the number of threads to numberOfCores * someFactor where someFactor depends on the I/O the Tasks perform.

You could also investigate custom TPL schedulers, I don't know much about that.