Book Review: .NET 4.5 Parallel Extensions Cookbook

.NET 4.5 Parallel Extensions Cookbook

Here’s my quick review of a new book, .NET 4.5 Parallel Extensions Cookbook by Bryan Freeman.

The book launches straight in to fairly long code examples, so it would be best if you’ve already got some basic experience using Tasks.

The book’s chapters are divided into sections providing many examples of the following topics:

  • The Task Parallel Library (TPL)
  • Parallel LINQ (PLINQ)
  • The new BCL concurrent collections and producer/consumers
  • Thread synronchronization primatives
  • The new Visual Studio parallel profiling and debugging windows
  • Async and await keywords
  • The new .NET Dataflows library

Reasons why you should read this book

If you already have basic familiarity with Tasks, but you aren’t sure the difference between:

  • Task.Start() and Task.Factory.StartNew()
  • Different TaskCreationOptions
  • Different ParallelMergeOptions for parallel LINQ queries

Or, if you’re not sure about:

  • The correct semantics around throwing OperationCancelledException
  • How to link multiple CancellationTokens together
  • How to schedule a continuation to execute when a task faults or is cancelled
  • How to break out of Parallel.For()
  • In the BCL concurrent collections, what completeness or a consuming iterator means
  • What is a Barrier and how to use it
  • What BufferBlock, TransformBlock, WriteOnceBlock, BroadcastBlock and JoinBlocks are for, and how they interact with Tasks

Then, like me, you will find this book useful.

Other bits I liked

The chapters on synchronization primatives provide not only an overview of new TPL concepts like Barriers, but also a good coverage of traditional .NET concepts (like Monitor and ManualResetEventSlim) and how they can be used inside Tasks.

The book highlights the very-useful new Visual Studio 2012 concurrency visualizer debugging windows, which can quickly identify performance issues in your program such as deadlocks or excessive context-switching (where one CPU core slows down because it is frequently reloading the cache and stack so it can switch to a different thread). These new features should be essential knowledge for any .NET developer working on multi-threaded code.

I also enjoyed the chapters on the new TPL Dataflow library, which provides a framework for building composable Task-based input/output processing steps, which can be linked together to form a larger sequential pipeline. Unfortunately, the book doesn’t provide many examples of situations when you might want to use Dataflows, but off the top of my head I can think of a couple of places in past projects where they would have been useful:

  • In an e-commerce system: post-order processing e.g. after a user makes a purchase, submitting the order to fulfilment, accounting, invoicing, analytics and other downstream systems.
  • In a trading/finance market risk analysis system: enriching trade positions through a series of steps that calculate their various different types of risk exposures (including calculations which can be quite CPU intensive) plus fetching other related information (OLAP dimensions) about the trade from other systems (which can involve IO-bound remote service/database calls).

Bits I didn’t like

It would have been nice if the author had further explored how Tasks can be used together with other popular .NET libraries, for example:

  • How Tasks compare to Reactive Extensions (Rx)?
  • How to create an IObservable event stream based on Task completions
  • How you can integrate Tasks into ASP.NET MVC and WebAPI to make scalable asynchronous controllers

The author also uses C_STYLE_CONSTANTS in his code examples — a minor (but annoying) stylistic mistake.

Also be aware the Kindle edition’s fonts are quite a lot smaller than typical books you might purchase from the Amazon store. I had to turn my Kindle display up to size 6 font in order to get an equivalent reading experience from my normal size 2 settings.

Otherwise, I didn’t notice any other serious problems, and would recommend the book as a good entry-level introduction for developers interested in learning more about the TPL.

Book links: Amazon (Paperback), Amazon (Kindle), Amazon (UK), Publisher’s Page

Disclaimer: this review was based on a free promotional copy sent to me by Tania at Packt Publishing.