Facilitating the spread of knowledge and innovation in professional software development



Choose your language

InfoQ Homepage News Patterns and Samples for .NET Parallel Extensions

Patterns and Samples for .NET Parallel Extensions


Even though Microsoft has been working on .NET’s Parallel Extensions since 2007, there are still many features that they didn’t have time to fully implement for .NET 4.0. Some features were “too application-specific to be included in the core of the Framework” while others simply needed for testing and user feedback. Stephen Toub continues,

Luckily, in the vast majority of these cases, we’ve been able to build this functionality on top of the parallelizatino constructs provided in .NET 4, in some cases simply containing .NET 4 types and methods, and in other cases taking advantage of extensibility points provided in .NET 4 for the explicit purpose of enabling developers to provide their own customized functionality where the Framework leaves off. Over time, we’ve amassed a wealth of code of this ilk, and we’ve chosen to expose most of it through the ParallelExtensionsExtras project available as part of our .NET 4 samples, which can be downloaded at

The code samples cover a wide range of functionality. Some of the highlights include:

An IProducerConsumerCollection implementation for the BlockingCollection wrapper. This interface is for thread-safe collections in traditional producer/consumer scenarios. This wasn’t included in .NET 4.0 because the correct semantics for a BlockingCollection version could vary depending on the application’s needs.

For working with COM components that require single-threaded apartments there is the StaTaskScheduler. The base version of the StaTaskScheduler is pretty simple, it simply spins up a predefined number of STA threads and waits for tasks to perform. A suggested variant is to use thread-local storage to instantiate one instance of a COM object for each worker thread.

For those interested in the Reactive Extensions Framework, there is the Task.ToObservable extension method. This allows a subscription to be attached to the result of a Task. If the subscription is dropped before the task is completed, then the task will be cancelled.

A rather complex scenario is the ReductionVariable. This contains a set of values, one per instance per thread, for use in reduction operations. Based on thread-local storage, it requires very little synchronization between worker threads and thus scales up well. Once the reduction operation is complete, the values can be enumerated over to generate the final result.

We need your feedback

How might we improve InfoQ for you

Thank you for being an InfoQ reader.

Each year, we seek feedback from our readers to help us improve InfoQ. Would you mind spending 2 minutes to share your feedback in our short survey? Your feedback will directly help us continually evolve how we support you.

Take the Survey

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p


Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.