Concurrency in C# Cookbook, 2nd Edition

Book Description

If you’re one of many developers still uncertain about concurrent and multithreaded development, this practical cookbook will change your mind. With more than 85 code-rich recipes in this updated second edition, author Stephen Cleary demonstrates parallel processing and asynchronous programming techniques using libraries and language features in .NET and C# 8.0.

Concurrency is now more common in responsive and scalable application development, but it’s still extremely difficult to code. The detailed solutions in this cookbook show you how modern tools raise the level of abstraction, making concurrency much easier than before. Complete with ready-to-use code and discussions about how and why solutions work, these recipes help you:

  • Get up to speed on concurrency and async and parallel programming
  • Use async and await for asynchronous operations
  • Enhance your code with asynchronous streams
  • Explore parallel programming with .NET’s Task Parallel Library
  • Create dataflow pipelines with .NET’s TPL Dataflow library
  • Understand the capabilities that System.Reactive builds on top of LINQ
  • Utilize threadsafe and immutable collections
  • Learn how to conduct unit testing with concurrent code
  • Make the thread pool work for you
  • Enable clean, cooperative cancellation
  • Examine scenarios for combining concurrent approaches
  • Dive into asynchronous-friendly object-oriented programming
  • Recognize and write adapters for code using older asynchronous styles

Table of Contents

  1. Preface
    1. Who Should Read This Book
    2. Why I Wrote This Book
    3. Navigating This Book
    4. Online Resources
    5. Conventions Used in This Book
    6. Using Code Examples
    7. O’Reilly Online Learning
    8. How to Contact Us
    9. Acknowledgments
  2. Concurrency: An Overview
    1. Introduction to Concurrency
    2. Introduction to Asynchronous Programming
    3. Introduction to Parallel Programming
    4. Introduction to Reactive Programming (Rx)
    5. Introduction to Dataflows
    6. Introduction to Multithreaded Programming
    7. Collections for Concurrent Applications
    8. Modern Design
    9. Summary of Key Technologies
  3. Async Basics
    1. 2.1. Pausing for a Period of Time
    2. 2.2. Returning Completed Tasks
    3. 2.3. Reporting Progress
    4. 2.4. Waiting for a Set of Tasks to Complete
    5. 2.5. Waiting for Any Task to Complete
    6. 2.6. Processing Tasks as They Complete
    7. 2.7. Avoiding Context for Continuations
    8. 2.8. Handling Exceptions from async Task Methods
    9. 2.9. Handling Exceptions from async void Methods
    10. 2.10. Creating a ValueTask
    11. 2.11. Consuming a ValueTask
  4. Asynchronous Streams
    1. Asynchronous Streams and Task<T>
    2. Asynchronous Streams and IEnumerable<T>
    3. Asynchronous Streams and Task<IEnumerable<T>>
    4. Asynchronous Streams and IObservable<T>
    5. Summary
    6. 3.1. Creating Asynchronous Streams
    7. 3.2. Consuming Asynchronous Streams
    8. 3.3. Using LINQ with Asynchronous Streams
    9. 3.4. Asynchronous Streams and Cancellation
  5. Parallel Basics
    1. 4.1. Parallel Processing of Data
    2. 4.2. Parallel Aggregation
    3. 4.3. Parallel Invocation
    4. 4.4. Dynamic Parallelism
    5. 4.5. Parallel LINQ
  6. Dataflow Basics
    1. 5.1. Linking Blocks
    2. 5.2. Propagating Errors
    3. 5.3. Unlinking Blocks
    4. 5.4. Throttling Blocks
    5. 5.5. Parallel Processing with Dataflow Blocks
    6. 5.6. Creating Custom Blocks
  7. System.Reactive Basics
    1. 6.1. Converting .NET Events
    2. 6.2. Sending Notifications to a Context
    3. 6.3. Grouping Event Data with Windows and Buffers
    4. 6.4. Taming Event Streams with Throttling and Sampling
    5. 6.5. Timeouts
  8. Testing
    1. 7.1. Unit Testing async Methods
    2. 7.2. Unit Testing async Methods Expected to Fail
    3. 7.3. Unit Testing async void Methods
    4. 7.4. Unit Testing Dataflow Meshes
    5. 7.5. Unit Testing System.Reactive Observables
    6. 7.6. Unit Testing System.Reactive Observables with Faked Scheduling
  9. Interop
    1. 8.1. Async Wrappers for “Async” Methods with “Completed” Events
    2. 8.2. Async Wrappers for “Begin/End” Methods
    3. 8.3. Async Wrappers for Anything
    4. 8.4. Async Wrappers for Parallel Code
    5. 8.5. Async Wrappers for System.Reactive Observables
    6. 8.6. System.Reactive Observable Wrappers for async Code
    7. 8.7. Asynchronous Streams and Dataflow Meshes
    8. 8.8. System.Reactive Observables and Dataflow Meshes
    9. 8.9. Converting System.Reactive Observables to Asynchronous Streams
  10. Collections
    1. 9.1. Immutable Stacks and Queues
    2. 9.2. Immutable Lists
    3. 9.3. Immutable Sets
    4. 9.4. Immutable Dictionaries
    5. 9.5. Threadsafe Dictionaries
    6. 9.6. Blocking Queues
    7. 9.7. Blocking Stacks and Bags
    8. 9.8. Asynchronous Queues
    9. 9.9. Throttling Queues
    10. 9.10. Sampling Queues
    11. 9.11. Asynchronous Stacks and Bags
    12. 9.12. Blocking/Asynchronous Queues
  11. Cancellation
    1. 10.1. Issuing Cancellation Requests
    2. 10.2. Responding to Cancellation Requests by Polling
    3. 10.3. Canceling Due to Timeouts
    4. 10.4. Canceling async Code
    5. 10.5. Canceling Parallel Code
    6. 10.6. Canceling System.Reactive Code
    7. 10.7. Canceling Dataflow Meshes
    8. 10.8. Injecting Cancellation Requests
    9. 10.9. Interop with Other Cancellation Systems
  12. Functional-Friendly OOP
    1. 11.1. Async Interfaces and Inheritance
    2. 11.2. Async Construction: Factories
    3. 11.3. Async Construction: The Asynchronous Initialization Pattern
    4. 11.4. Async Properties
    5. 11.5. Async Events
    6. 11.6. Async Disposal
  13. Synchronization
    1. 12.1. Blocking Locks
    2. 12.2. Async Locks
    3. 12.3. Blocking Signals
    4. 12.4. Async Signals
    5. 12.5. Throttling
  14. Scheduling
    1. 13.1. Scheduling Work to the Thread Pool
    2. 13.2. Executing Code with a Task Scheduler
    3. 13.3. Scheduling Parallel Code
    4. 13.4. Dataflow Synchronization Using Schedulers
  15. Scenarios
    1. 14.1. Initializing Shared Resources
    2. 14.2. System.Reactive Deferred Evaluation
    3. 14.3. Asynchronous Data Binding
    4. 14.4. Implicit State
    5. 14.5. Identical Synchronous and Asynchronous Code
    6. 14.6. Railway Programming with Dataflow Meshes
    7. 14.7. Throttling Progress Updates
  16. Legacy Platform Support
    1. Legacy Platform Support for Async
    2. Legacy Platform Support for Dataflow
    3. Legacy Platform Support for System.Reactive
  17. Recognizing and Interpreting Asynchronous Patterns
    1. Task-Based Asynchronous Pattern (TAP)
    2. Asynchronous Programming Model (APM)
    3. Event-Based Asynchronous Programming (EAP)
    4. Continuation Passing Style (CPS)
    5. Custom Async Patterns
    6. ISynchronizeInvoke
  18. Index

Product Information

  • Title: Concurrency in C# Cookbook, 2nd Edition
  • Author(s): Stephen Cleary
  • Release date: August 2019
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492054498