Chapter 22. Parallel Programming
In this chapter, we cover the multithreading APIs and constructs aimed at leveraging multicore processors:
Parallel LINQ or PLINQ
The
Parallel
classThe task parallelism constructs
The concurrent collections
SpinLock
andSpinWait
These are new to Framework 4.0 and are collectively known (loosely)
as PFX (Parallel Framework). The Parallel
class together with the task
parallelism constructs is called the Task Parallel
Library or TPL.
Framework 4.0 also adds a number of lower-level threading constructs that are aimed equally at traditional multithreading. We covered these in the previous chapter:
The low-latency signaling constructs:
SemaphoreSlim
(see the section Locking)ManualResetEventSlim
(see the section Signaling with Event Wait Handles)CountdownEvent
(see the section Signaling with Event Wait Handles)Barrier
(see the section The Barrier Class)
Cancellation tokens for cooperative cancellation (see the section Safe Cancellation)
The lazy initialization classes (see the section Lazy Initialization)
ThreadLocal<T>
(see the section Thread-Local Storage)
You’ll need to be comfortable with the fundamentals in Chapter 21 before reading this chapter—particularly locking and thread safety.
Why PFX?
In recent times, CPU clock speeds have stagnated and manufacturers have shifted their focus to increasing core counts. This is problematic for us as programmers because our standard single-threaded code will not automatically run faster as a result of those extra cores.
Leveraging ...
Get C# 4.0 in a Nutshell, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.