I’ve been reading about some of the new support for parallelism and concurrency in version 4 of the .NET Framework, and came across a really good paper on parallel design patterns by Steven Toub (most or all of which seems to also be covered in the Parallel Programming with Microsoft .NET book). There are some really convenient additions to the framework in .NET 4 that help make parallelism easier and less error-prone. The point of this post is to illuminate a good example of how this is so.
About a year ago, I was working on a class library that was supposed to do some filtering on streams – something along the lines of Boost.Iostreams which gives you the ability to create pluggable, customized “sources” and “sinks” (producers and consumers) and filters set up in a stream pipeline. One simple application of such a pipeline (and apparently Toub’s favorite) is that of compressing and encrypting some data.
I found Toub’s MSDN Magazine article describing how one could parallelize this, and incorporated the sample into my class library. The basic idea is that you set up a blocking queue of chunks of data, and then use that queue (or queues) to connect the inputs and outputs of the streams and filters, and just let it fly. The queue serves as the synchronization mechanism, fulfilling the producer role required by the input side of each piece, and fulfilling the consumer role required by the output side of each piece. Below is the sample code.

Andrew Connell
Contact