GrowinMan
GrowinMan

Reputation: 4907

Is my understanding of the C# threadpool correct?

I'm reading Essential C# 5.0 which says,

The thread pool also assumes that all the work will be relatively short-running (that is, consuming milliseconds or seconds of processor time, not hours or days). By making this assumption, it can ensure that each processor is working full out on a task, and not inefficiently time slicing multiple tasks. The thread pool attempts to prevent excessive time slicing by ensuring that thread creation is "throttled" and so that no one processor is "oversubscrived" with too many threads.

I've always thought one of the benefits of multithreading was time slicing.

If you >1 processor, than you can concurrently run threads and achieve true multithreading. But unless that's the case, you'd have to resort to time slicing for applications with multiple threads right?

So, if the ThreadPool in C# doesn't time slice, then,

a. Does it mean the ThreadPool is only used to get around the overhead in creating new threads?

b. Does it mean the ThreadPool can't run multiple threads simultaneously unless the processor has multiple cores, where each core can run a single process?

Upvotes: 2

Views: 445

Answers (3)

Vladislav Vaintroub
Vladislav Vaintroub

Reputation: 5653

Q. Does it mean the ThreadPool is only used to get around the overhead in creating new threads?

A. No. Thread creation is usually not the problem. The goal is make your CPU work at 100%, while using as as few concurrently executing threads as it is possible. The low number of threads will avoid excessive context switching, and improve overall execution time.

Q. Does it mean the ThreadPool can't run multiple threads simultaneously unless the processor has multiple cores, where each core can run a single process?

A. It runs multiple threads simultaneously. Ideally it runs exactly one CPU-intensive task per core, in which case you CPU works at 100%

Upvotes: 0

Cory Nelson
Cory Nelson

Reputation: 29981

The .NET thread pool will create multiple threads per core, but has heuristics to keep the number of threads as low as possible while performing the maximum amount of work.

This means if your code is CPU-bound, you may end up with a single thread per core. If your code is I/O-bound and blocks, or queues up a huge amount of work items, you may end up with many threads per core.

It's not just thread creation that is expensive: context switching between hundreds of threads takes up a lot of time that you'd rather be spent running your own code. More threads is almost never better.

Upvotes: 3

Chris Shain
Chris Shain

Reputation: 51309

The quote you mention refers to the rate at which the Threadpool creates new threads when all of the threads that it has already created are already allocated to a task. The new thread creation rate is throttled (and new tasks are queued) to avoid creating many threads (and swamping the CPU) when a large burst of tasks are put on the Threadpool.

The current algorithm does indeed create many threads per CPU core, but it creates them relatively slowly, in the hope that the current backlog of tasks will be quickly satisfied by the threads that it has already created, and adding threads will not be needed.

Upvotes: 2

Related Questions