Disclosure: I was offered a free electronic copy of this book in exchange for this book review. The publisher had no input on the review.
Parallel processing is one of the most challenging problems for the modern developer. The first time you start creating threads, you open yourself up to a whole host of problems, including deadlocks and data corruption. The ASP.NET team went to great lengths to hide all the difficulties of multi-threading by presenting a programming model that allows developers to reap the benefits of multi-threading without sweating most of the details. Similarly, the Task Parallel Library – introduced in .NET 4, and greatly improved in .NET 4.5 – attempts to bring that same level of abstraction without diving deep into the threading details.
With .NET 4.5, C# and VB acquired new keywords (async and await) that make the creation and consumption of Tasks simpler. Most lengthy operations in the .NET framework were enhanced to support returning Tasks. Using the async and await keywords makes it trivial to write sequential code that leverages asynchronous behavior, and since threads are not blocked, user interfaces can remain responsive. Microsoft made Tasks the foundation of long running operations in their Windows 8 and Windows Phone 8 programming model to ensure that users would always get smooth UI interactions.
“.NET 4.5 Parallel Extensions Cookbook” by Bryan Freeman is a book that covers all aspects of the new Task Parallel Library (including the out-of-band Dataflow Library). The chapters topics include:
- The basics of the Task class, and cancellation with CancellationTokens
- Task continuations and child Tasks
- Parallel data process with Parallel.For and Parallel.ForEach
- Parallelization of LINQ queries
- Concurrent collections like ConcurrentDictionary
- Synchronization primitives like mutexes, monitors, and reader/writer locks
- Profiling and debugging parallel code
- Using async and await
- The Dataflow Library for pluggable parallel data processing
Each chapter includes comprehensive example code on using the feature in question. Many of the samples cover very similar ground, which helps to focus the reader on understanding just the technology at hand.
True to cookbook style, the book focuses mostly on the “how”, and leaves most of the “why” to the reader. This makes for a concise code-focused read, but it can leave the reader wondering which technique is best to employ in a particular situation.
On the plus side, the debugging and profiling chapter is worth its weight in gold. Understanding how to employ the multi-threading debugging tools can be black magic, so this chapter is extremely welcome when something goes wrong. The coverage of the Dataflow Library is also unique, as most developers have probably never heard of it (since it was released out-of-band). The thoroughness of the chapters is also one of the book’s stronger suits.
On the down side, the book appears to be an adaptation of a .NET 4.0 edition to 4.5, as the final two chapters seem added on to the existing material. Unfortunately, this means the first two chapters – learning about Tasks and continuations – introduce the developer to techniques that are often inappropriate when targeting 4.5. I would’ve preferred to see those first two chapters re-written in 4.5 style rather than left as-is, so my recommendation to readers would be to immediately read chapter 9 after chapters 1 and 2, and throw away much of the advice from those early chapters.
On the whole, this book can be valuable to someone looking for concentrated information about the TPL. As mentioned previously, if you have little experience with debugging and profiling multithreaded code, that chapter is worth the price of entry alone.