Skip to content

Latest commit

 

History

History
54 lines (37 loc) · 1.21 KB

02-threadpool.md

File metadata and controls

54 lines (37 loc) · 1.21 KB

02 - ThreadPool

Goal

Understand that .NET avoids creating a new OS thread for every piece of work by using a pool of reusable worker threads.

Task

Create a scheduler that runs many small jobs using:

  • ThreadPool.QueueUserWorkItem
  • Task.Run
  • sequential execution

Suggested interface

public interface IWorkScheduler
{
    Task RunAsync(
        IReadOnlyCollection<Action> jobs,
        CancellationToken cancellationToken);
}

Step-by-step

  1. Create IWorkScheduler.
  2. Implement SequentialWorkScheduler.
  3. Implement ThreadPoolWorkScheduler.
  4. Implement TaskWorkScheduler.
  5. Add tests proving all jobs are executed.
  6. Add benchmark with 100, 1,000, and 10,000 jobs.

Important experiment

Create jobs that only increment an atomic counter.

Then compare:

  • raw Thread
  • ThreadPool
  • Task

Questions to answer in this document

  • Why is raw thread creation expensive?
  • Why does ThreadPool reuse threads?
  • What is the difference between queueing work and waiting for work?
  • What happens when jobs block for a long time?

Key takeaway

The ThreadPool is good for many short-lived units of work. It is not magic; blocking ThreadPool threads can still hurt the application.