Cutting Costs

The best way to think about threads is as virtual processors. They provide a level of abstraction on top of physical processors, and it’s the operating system’s task to switch between different threads such that all of them can make progress. The mechanism by which a thread is swapped out for another one to run is known as a context switch.

Context switches are expensive, and so is thread creation. So why would one create a plethora of threads as a means to run things in parallel? This question was exactly the motivation for introducing pools of threads. This way, the number of threads can be limited, reducing context switching. Pooling also allows for reuse of threads to carry out different work items drawn from a work item queue. ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.