Book Image

.NET 4.5 Parallel Extensions Cookbook

By : Bryan Freeman
Book Image

.NET 4.5 Parallel Extensions Cookbook

By: Bryan Freeman

Overview of this book

<p>.NET parallel extensions brings the power of parallel and asynchronous programming to a much wider developer audience than ever before. This book will give a developer with no multithreaded development experience the ability to write highly scalable parallel applications that take advantage of modern multicore processors.If you are an experienced .NET developer who wants to put parallel extensions to work in your applications, this book is for you.</p> <p>".NET 4.5 Parallel Extensions Cookbook" is a practical, hands-on guide that provides you with a number of clear step-by-step recipes that will introduce parallelism into your applications and take advantage of modern multicore processors. This book is a crash course in using the extensions, with theory and concepts kept to a minimum.</p> <p>".NET 4.5 Parallel Extensions Cookbook" offers a wide-ranging presentation of parallel development concepts, and provides a working knowledge of key technologies that are important to developers who want to take advantage of multi-core architectures.</p> <p>You will learn how to compose a series of producer/consumer tasks into a pipeline that can process data elements received from a real-time event stream. You will also learn how to connect the stages of pipelines together using the concurrent collections. You will learn everything you need to know to transform the multicore power found in modern processors into application performance and scalability.</p>
Table of Contents (16 chapters)
.NET 4.5 Parallel Extensions Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

The multicore processor boom is fully underway. It is becoming difficult to find a server, desktop, or laptop that doesn't have at least two processor cores. There are even several models of dual core mobile phones on the market.

However, to fully realize the power of modern multicore processors, developers need to be able to effectively recognize opportunities for parallelization and write parallel code.

Largely due to the complexities of creating and managing the lifecycle of classic threads, parallel coding is not routinely practiced by the large majority of developers.

To help ease the complexities of writing parallel code, Microsoft has introduced the Parallel Extensions in .NET framework. The Parallel Extensions or Task Parallel Library helps us to write efficient, scalable, parallel code in a natural manner without having to deal directly with the classic threading model or the thread pool. Using these extensions, we can conveniently write parallel versions of existing sequential code that can automatically utilize all available processors, thus providing significant performance gains.

This book provides coverage of the major classes of the .Net 4.5 Parallel extensions from front to back, and will provide a developer with no multithreaded development experience ability to write highly scalable asynchronous applications that take advantage of today's hardware.

What this book covers

Chapter 1, Getting Started with Task Parallel Library, looks at the various ways to create and start a task, cancel a task, handle exceptions in a task, and control the behavior of a task. At the core of the .NET Parallel Extensions is the System.Threading.Task class.

Chapter 2, Implementing Continuations, helps us learn how to create task continuations and child tasks. We will also learn how to control the condition under which continuations run, how to handle exceptions using continuations, and how to chain tasks and continuations together to form a pipeline pattern. In asynchronous programming, it is very common for one asynchronous operation to invoke a second operation and pass data to it.

Chapter 3, Learning Concurrency with Parallel Loops, helps us learn how to use parallel loops to process large collections of data in parallel. We will also learn how to control the degree of parallelism in loops, break out of loops, and partition source data for loop processing.

Chapter 4, Parallel LINQ, helps us learn how to create a parallel LINQ query, control query options such as results merging, and the degree of parallelism for the query. Parallel LINQ is a parallel implementation of LINQ to Objects.

Chapter 5, Concurrent Collections, helps us learn how to use concurrent collections as a data buffer in a producer-consumer pattern, how to add blocking and bounding to a custom collection, and how to use multiple concurrent collections for forming a pipeline. Concurrent collections in .NET Framework 4.5 allows developers to create type-safe as well as thread-safe collections.

Chapter 6, Synchronization Primitives, helps us look at the synchronization primitives available in the .NET Framework, and how to use the new lightweight synchronization primitives. Parallel applications usually need to manage access to some kind of shared data.

Chapter 7, Profiling and Debugging, helps us examine the parallel debugging and profiling tools build into Visual Studio 2012, and how to use them to debug and optimize our parallel applications.

Chapter 8, Async, helps us learn about the Async feature of the .NET Framework, and using asynchrony to maintain a responsive UI. We've all seen client applications that don't respond to mouse events or update the display for noticeable periods of time due to synchronous code holding on to the single UI thread for too long.

Chapter 9, Dataflow Library, helps us examine the various types of dataflow blocks provided by Microsoft, and how to use them to form a data processing pipeline. The Task Parallel Library's dataflow library is a new set of classes that are designed to increase the robustness of highly concurrent applications by using asynchronous message passing and pipelining to obtain more control and better performance than manual threading.

What you need for this book

This book assumes you have a solid foundation in general C# development. You should have at least a working knowledge of lambda expressions and delegates. You will need a Windows 7, Windows 8, or Windows Server 2008 R2 PC. A multicore or multiprocessor machine is not required, but is preferable. In addition, you will need Visual Studio 2012 and the .NET Framework 4.5.

Who this book is for

This book is intended to help experienced C# developers write applications that leverage the power of modern multicore processors. It provides the necessary knowledge for an experienced C# developer to work with .NET Parallel Extensions. Previous experience of writing multithreaded applications is not necessary.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "In this recipe, we will take a look at the basics of adding items to, and removing items from BlockingCollection."

A block of code is set as follows:

char[] delimiters = { ' ', ',', '.', ';', ':', '-', '_', '/', '\u000A' };
const string headerText = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

[var result = Enumerable.Range(0, 10000).AsParallel()
        .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
        .WithDegreeOfParallelism(2)
        …
        .Select((x, i) => i)
        .ToArray();                       

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "clicking the Next button moves you to the next screen".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.