Concurrency in C# Cookbook
Stephen Cleary
Praise for Concurrency in C# Cookbook
The next big thing in computing is making massive parallelismaccessible to mere mortals. Developers have more power available to usthan ever before, but expressing concurrency is still a challenge formany. Stephen turns his attention to this problem, helping us allbetter understand concurrency, threading, reactive programming models,parallelism, and much more in an easy-to-read but complete reference.
Scott HanselmanPrincipal Program Manager, ASP.NET and Azure Web Tools, Microsoft
The breadth of techniques covered and the cookbook format make thisthe ideal reference book for modern .NET concurrency.
Jon SkeetSenior Software Engineer at Google
Stephen Cleary has established himself as a key expert on asynchronyand parallelism in C#. This book clearly and concisely conveys themost important points and principles developers need to understand toget started and be successful with these technologies.
Stephen ToubPrincipal Architect, Microsoft
Preface
I think the animal on this cover, a common palm civet, is applicable to the subject of this book. I knew nothing about this animal until I saw the cover, so I looked it up. Common palm civets are considered pests because they defecate all over ceilings and attics and make loud noises fighting with each other at the most inopportune times. Their anal scent glands emit a nauseating secretion. They have an endangered species rating of Least Concern, which is apparently the politically correct way of saying, Kill as many of these as you want; no one will miss them. Common palm civets enjoy eating coffee cherries, and they pass the coffee beans through. Kopi luwak, one of the most expensive coffees in the world, is made from the coffee beans extracted from civet excretions. According to the Specialty Coffee Association of America, It just tastes bad.
This makes the common palm civet a perfect mascot for concurrent and multithreaded developement. To the uninitiated, concurrency and multithreading are undesirable. They make well-behaved code act up in the most horrendous ways. Race conditions and whatnot cause loud crashes (always, it seems, either in production or a demo). Some have gone so far as to declare threads are evil and avoid concurrency completely. There are a handful of developers who have developed a taste for concurrency and use it without fear; but most developers have been burned in the past by concurrency, and that experience has left a bad taste in their mouth.
However, for modern applications, concurrency is quickly becoming a requirement. Users these days expect fully responsive interfaces, and server applications are having to scale to unprecedented levels. Concurrency addresses both of these trends.
Fortunately, there are many modern libraries that make concurrency much easier! Parallel processing and asynchronous programming are no longer exclusively the domains of wizards. By raising the level of abstraction, these libraries make responsive and scalable application development a realistic goal for every developer. If you have been burned in the past when concurrency was extremely difficult, then I encourage you to give it another try with modern tools. We can probably never call concurrency easy, but it sure isnt as hard as it used to be!
Who Should Read This Book
This book is written for developers who want to learn modern approaches to concurrency. I do assume that youve got a fair amount of .NET experience, including an understanding of generic collections, enumerables, and LINQ. I do not expect that you have any multithreading or asynchronous programming knowledge. If you do have some experience in those areas, you may still find this book helpful because it introduces newer libraries that are safer and easier to use.
Concurrency is useful for any kind of application. It doesnt matter whether you work on desktop, mobile, or server applications; these days concurrency is practically a requirement across the board. You can use the recipes in this book to make user interfaces more responsive and servers more scalable. We are already at the point where concurrency is ubiquitous, and understanding these techniques and their uses is essential knowledge for the professional developer.
Why I Wrote This Book
Early in my career, I learned multithreading the hard way. After a couple of years, I learned asynchronous programming the hard way. While those were both valuable experiences, I do wish that back then I had some of the tools and resources that are available today. In particular, the async
and await
support in modern .NET languages is pure gold.
However, if you look around today at books and other resources for learning concurrency, they almost all start by introducing the most low-level concepts. Theres excellent coverage of threads and serialization primitives, and the higher-level techniques are put off until later, if theyre covered at all. I believe this is for two reasons. First, many developers of concurrency such as myself did learn the low-level concepts first, slogging through the old-school techniques. Second, many books are years old and cover now-outdated techniques; as the newer techniques have become available, these books have been updated to include them, but unfortunately placed them at the end.
I think thats backward. In fact, this book only covers modern approaches to concurrency. Thats not to say theres no value in understanding all the low-level concepts. When I went to college for programming, I had one class where I had to build a virtual CPU from a handful of gates, and another class that covered assembly programming. In my professional career, Ive never designed a CPU, and Ive only written a couple dozen lines of assembly, but my understanding of the fundamentals still helps me every day. However, its best to start with the higher-level abstractions; my first programming class was not in assembly language.
This book fills a niche: it is an introduction to (and reference for) concurrency using modern approaches. It covers several different kinds of concurreny, including parallel, asynchronous, and reactive programming. However, it does not cover any of the old-school techniques, which are adequately covered in many other books and online resources.
Navigating This Book
This book is intended as both an introduction and as a quick reference for common solutions. The book is broken down as follows:
- Chapter 1 is an introduction to the various kinds of concurrency covered by this book: parallel, asynchronous, reactive, and dataflow.
- Chapters 2-5 are a more thorough introduction to these kinds of concurrency.
- The remaining chapters each deal with a particular aspect of concurrency, and act as a reference for solutions to common problems.
I recommend reading (or at least skimming) the first chapter, even if youre already familiar with some kinds of concurrency.
Online Resources
This book acts like a broad-spectrum introduction to several different kinds of concurrency. Ive done my best to include techniques that I and others have found the most helpful, but this book is not exhaustive by any means. The following resources are the best ones Ive found for a more thorough exploration of these technologies.
For parallel programming, the best resource I know of is Parallel Programming with Microsoft .NET by Microsoft Press, which is available online. Unfortunately, it is already a bit out of date. The section on Futures should use asynchronous code instead, and the section on Pipelines should use TPL Dataflow.