Concurrency is essential for creating fast apps and optimized systems software today. With this cookbook, you will find recipes for writing asynchronous, parallel, and reactive code in C# that enables your app or program to engage in more than one process at a time. Presented in O'Reilly's popular problem-solution-discussion cookbook format, this guide provides ready-to-use code, along with an explanation of why and how the solution works.
Preface vii
1 Concurrency: An Overview 1 (18)
1.1 Introduction to Concurrency 1 (2)
1.2 Introduction to Asynchronous 3 (4)
Programming
1.3 Introduction to Parallel Programming 7 (3)
1.4 Introduction to Reactive Programming 10 (2)
(Rx)
1.5 Introduction to Dataflows 12 (2)
1.6 Introduction to Multithreaded 14 (1)
Programming
1.7 Collections for Concurrent 15 (1)
Applications
1.8 Modern Design 15 (1)
1.9 Summary of Key Technologies 16 (3)
2 Async Basics 19 (18)
2.1 Pausing for a Period of Time 20 (2)
2.2 Returning Completed Tasks 22 (1)
2.3 Reporting Progress 23 (1)
2.4 Waiting for a Set of Tasks to Complete 24 (3)
2.5 Waiting for Any Task to Complete 27 (1)
2.6 Processing Tasks as They Complete 28 (4)
2.7 Avoiding Context for Continuations 32 (1)
2.8 Handling Exceptions from async Task 33 (1)
Methods
2.9 Handling Exceptions from async Void 34 (3)
Methods
3 Parallel Basics 37 (10)
3.1 Parallel Processing of Data 37 (2)
3.2 Parallel Aggregation 39 (2)
3.3 Parallel Invocation 41 (1)
3.4 Dynamic Parallelism 42 (2)
3.5 Parallel LINQ 44 (3)
4 Dataflow Basics 47 (10)
4.1 Linking Blocks 48 (1)
4.2 Propagating Errors 49 (2)
4.3 Unlinking Blocks 51 (1)
4.4 Throttling Blocks 52 (1)
4.5 Parallel Processing with Dataflow 53 (1)
Blocks
4.6 Creating Custom Blocks 54 (3)
5 Rx Basics 57 (12)
5.1 Converting NET Events 58 (2)
5.2 Sending Notifications to a Context 60 (2)
5.3 Grouping Event Data with Windows and 62 (2)
Buffers
5.4 Taming Event Streams with Throttling 64 (2)
and Sampling
5.5 Timeouts 66 (3)
6 Testing 69 (14)
6.1 Unit Testing async Methods 70 (1)
6.2 Unit Testing async Methods Expected 71 (2)
to Fail
6.3 Unit Testing async void Methods 73 (1)
6.4 Unit Testing Dataflow Meshes 74 (2)
6.5 Unit Testing Rx Observables 76 (2)
6.6 Unit Testing Rx Observables with 78 (5)
Faked Scheduling
7 Interop 83 (12)
7.1 Async Wrappers for "Async" Methods 83 (2)
with "Completed" Events
7.2 Async Wrappers for "Begin/End" methods 85 (1)
7.3 Async Wrappers for Anything 86 (2)
7.4 Async Wrappers for Parallel Code 88 (1)
7.5 Async Wrappers for Rx Observables 89 (1)
7.6 Rx Observable Wrappers for async Code 90 (2)
7.7 Rx Observables and Dataflow Meshes 92 (3)
8 Collections 95 (26)
8.1 Immutable Stacks and Queues 98 (2)
8.2 Immutable Lists 100(2)
8.3 Immutable Sets 102(2)
8.4 Immutable Dictionaries 104(2)
8.5 Threadsafe Dictionaries 106(2)
8.6 Blocking Queues 108(2)
8.7 Blocking Stacks and Bags 110(2)
8.8 Asynchronous Queues 112(3)
8.9 Asynchronous Stacks and Bags 115(2)
8.10 Blocking/Asynchronous Queues 117(4)
9 Cancellation 121(16)
9.1 Issuing Cancellation Requests 122(3)
9.2 Responding to Cancellation Requests 125(1)
by Polling
9.3 Canceling Due to Timeouts 126(1)
9.4 Canceling async Code 127(1)
9.5 Canceling Parallel Code 128(2)
9.6 Canceling Reactive Code 130(2)
9.7 Canceling Dataflow Meshes 132(1)
9.8 Injecting Cancellation Requests 133(1)
9.9 Interop with Other Cancellation 134(3)
Systems
10 Functional-Friendly OOP 137(18)
10.1 Async Interfaces and Inheritance 137(2)
10.2 Async Construction: Factories 139(2)
10.3 Async Construction: The Asynchronous 141(3)
Initialization Pattern
10.4 Async Properties 144(3)
10.5 Async Events 147(3)
10.6 Async Disposal 150(5)
11 Synchronization 155(14)
11.1 Blocking Locks 160(2)
11.2 Async Locks 162(2)
11.3 Blocking Signals 164(1)
11.4 Async Signals 165(2)
11.5 Throttling 167(2)
12 Scheduling 169(6)
12.1 Scheduling Work to the Thread Pool 169(2)
12.2 Executing Code with a Task Scheduler 171(2)
12.3 Scheduling Parallel Code 173(1)
12.4 Dataflow Synchronization Using 174(1)
Schedulers
13 Scenarios 175(8)
13.1 Initializing Shared Resources 175(2)
13.2 Rx Deferred Evaluation 177(1)
13.3 Asynchronous Data Binding 178(2)
13.4 Implicit State 180(3)
Index 183