PacktLib: .NET 4.5 Parallel Extensions Cookbook

.NET 4.5 Parallel Extensions Cookbook


About the Author

About the Reviewers


Getting Started with Task Parallel Library


Creating a task

Waiting for tasks to finish

Returning results from a task

Passing data to a task

Creating a child task

Lazy task execution

Handling task exceptions using try/catch block

Handling task exceptions with AggregateException.Handle

Cancelling a task

Cancelling one of many tasks

Implementing Continuations


Continuing a task

Passing task results to a continuation

Continue "WhenAny" and "WhenAll"

Specifying when a continuation will run

Using a continuation for exception handling

Cancelling a continuation

Using a continuation to chain multiple tasks

Using a continuation to update a UI

Learning Concurrency with Parallel Loops


Creating a basic parallel for loop

Creating a basic parallel foreach loop

Breaking a parallel loop

Stopping a parallel loop

Cancelling a parallel loop

Handling exceptions in a parallel loop

Controlling the degree of parallelism in a loop

Partitioning data in a parallel loop

Using Thread Local Storage

Parallel LINQ


Creating a basic parallel query

Preserving order in parallel LINQ

Forcing parallel execution

Limiting parallelism in a query

Processing query results

Specifying merge options

Range projection with parallel LINQ

Handling exceptions in parallel LINQ

Cancelling a parallel LINQ query

Performing reduction operations

Creating a custom partitioner

Concurrent Collections


Adding and removing items to BlockingCollection

Iterating a BlockingCollection with GetConsumingEnumerable

Performing LIFO operations with ConcurrentStack

Thread safe data lookups with ConcurrentDictionary

Cancelling an operation in a concurrent collection

Working with multiple producers and consumers

Creating object pool with ConcurrentStack

Adding blocking and bounding with IProducerConsumerCollection

Using multiple concurrent collections to create a pipeline

Synchronization Primitives


Using monitor

Using a mutual exclusion lock

Using SpinLock for synchronization

Interlocked operations

Synchronizing multiple tasks with a Barrier

Using ReaderWriterLockSlim

Using WaitHandles with Mutex

Waiting for multiple threads with CountdownEvent

Using ManualResetEventSlim to spin and wait

Using SemaphoreSlim to limit access

Profiling and Debugging


Using the Threads and Call Stack windows

Using the Parallel Stacks window

Watching values in a thread with Parallel Watch window

Detecting deadlocks with the Parallel Tasks window

Measure CPU utilization with Concurrency Visualizer

Using Concurrency Visualizer Threads view

Using Concurrency Visualizer Cores view



Creating an async method

Handling Exceptions in asynchronous code

Cancelling an asynchronous operation

Cancelling async operation after timeout period

Processing multiple async tasks as they complete

Improving performance of async solution with Task.WhenAll

Using async for file access

Checking the progress of an asynchronous task

Dataflow Library


Reading from and writing to a dataflow block synchronously

Reading from and writing to a dataflow block asynchronously

Implementing a producer-consumer dataflow pattern

Creating a dataflow pipeline

Cancelling a dataflow block

Specifying the degree of parallelism

Unlink dataflow blocks

Using JoinBlock to read from multiple data sources