Praise for Concurrency in C# Cookbook, Second Edition
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 Hanselman, Principal 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 Skeet, Senior 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 Toub, Principal Architect, Microsoft
Concurrency in C# Cookbook, Second Edition
By Stephen Cleary
Copyright 2019 Stephen Cleary. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Acquisitions Editor: Chris Guzikowski
- Development Editor: Corbin Collins
- Production Editor: Deb Baker
- Copyeditor: Amanda Kersey
- Proofreader: Sonia Saruba
- Indexer: Angela Howard
- Interior Designer: David Futato
- Cover Designer: Randy Comer
- Illustrator: Rebecca Demarest
- June 2014: First Edition
- August 2019: Second Edition
Revision History for the Second Edition
- 2019-08-20: First release
See http://oreilly.com/catalog/errata.csp?isbn=9781492054504 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Concurrency in C# Cookbook, Second Edition, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
The views expressed in this work are those of the author, and do not represent the publishers views. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-492-05450-4
[LSI]
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 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 development. 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 during 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 have 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. Still, its best to start with the higher-level abstractions; my first programming class wasnt in assembly language.