If you need to understand concurrency, this book is your guide to the fundamentals behind the advanced software you seek to implement to achieve highly responsive and scalable code. Support for parallel computation is an essential part of concurrency. Concurrency is an advanced concept and solutions are not straightforward. Many developers have been burned by it and are still being burned by it. This book aims to simplify the concept for C# developers. It tries to simplify the concept using the Task Parallel Library (TPL), Concurrent Collections, Parallel LINQ (PLINQ), Asynchronous Programming Patterns, and related topics.
The book starts with an overview of TPL and discusses Tasks. Understanding these areas is necessary to learn the concepts that follow in the book. You will go through special scenarios, such as handling exceptions and cancellations, followed by demonstrations of synchronization techniques and concurrent collections. You will see demonstrations of parallel loops to speed up the computations. And you'll understand PLINQ in detail. Finally, you'll learn how to simplify asynchronous programming with async and await keywords is discussed.
The book contains “Q&A sessions”, review questions, and exercises (in .NET 8 and C#12). After reading the book, you will be able to understand advanced concepts in parallel programming and implement them in your code.
What You Will Learn
• Understand concurrent and multi-threaded development
• Understand how some modern-day C# features can promote parallel programming
• Demonstrate the latest patterns for parallel development
Who This Book Is For
Developers familiar with C# but are absolute beginners for parallel programming.
About the Author
Vaskaran Sarcar obtained his Master of Engineering degree in Software Engineering from Jadavpur University, Kolkata (India), and an MCA from Vidyasagar University, Midnapore (India). He was a National Gate Scholar (2007-2009)
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
Tip the Site
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat Pay
Alipay
Open WeChat or Alipay and scan. No login required.
AI guide
【One-Line Pitch】
A practical, example-driven guide for C# developers who want to move from theory to working code in parallel and asynchronous programming, covering the Task Parallel Library, synchronization, concurrent collections, and async/await with .NET 8 and C# 12.
【Book Arc】
- **Opening (~0%–9%)**: Sets the stage with the book's philosophy—there's "no royal road" to parallel programming—and introduces the core toolkit: TPL, Tasks, Concurrent Collections, PLINQ, and async/await. It frames the reader as a C# developer who is new to concurrency and warns against quick-fix learning.
- **Early (~9%–25%)**: Dives into Task fundamentals: five ways to create and start tasks, passing values (including the boxing trade-off), retrieving results via `Task<T>.Result`, and chaining work with `ContinueWith` and `TaskContinuationOptions` (e.g., `OnlyOnRanToCompletion`, `OnlyOnCanceled`). Also covers waiting patterns like `WaitAll` and `WaitAny`.
- **Early–Middle (~25%–38%)**: Handles special scenarios: exception handling with `AggregateException` (single vs. multiple locations), cancellation via `CancellationTokenSource`, and the `TaskCompletionSource<T>` pattern for bridging async and sync code. Includes exercises that clarify subtle behaviors, such as why `WaitAny` doesn't propagate exceptions.
- **Middle (~38%–47%)**: Explores synchronization and concurrent collections. Starts with a non-synchronized bank account example showing race conditions, then moves to thread-safe types in `System.Collections.Concurrent`: `ConcurrentStack<T>`, `ConcurrentQueue<T>`, `ConcurrentDictionary<TKey,TValue>`, `ConcurrentBag<T>`, and `BlockingCollection<T>`, plus the `IProducerConsumerCollection<T>` interface.
- **Late (~47%–100%)**: Covers parallel loops (e.g., `Parallel.For`, `Parallel.ForEach`) to speed up CPU-bound work, PLINQ for declarative parallel queries, and finally async/await—including state machines behind the scenes, the difference between `Task.Run` and `Task.Factory.StartNew`, and unwrapping nested tasks with `Unwrap` or `await`. Ends with exercises and solutions.
【Key Takeaways】
- **Tasks are the foundation of concurrency** (Early): Five creation patterns (e.g., `new Task`, `Task.Run`, `Task.Factory.StartNew`) each have trade-offs—passing `object` to methods causes boxing/unboxing, while lambdas are cleaner but less explicit. Choose based on clarity vs. performance.
- **Continuations give you fine-grained control** (Early): `ContinueWith` with `TaskContinuationOptions` (e.g., `OnlyOnRanToCompletion`, `OnlyOnCanceled`) lets you attach success/cancel handlers, making task status explicit and reducing manual checks.
- **Exceptions in tasks are aggregated, not thrown directly** (Early–Middle): `AggregateException` wraps all failures; use `ae.Handle()` to filter and process them in one place or propagate up the call stack. This avoids crashing the app and centralizes error logic.
- **Cancellation is cooperative, not forceful** (Early–Middle): Use `CancellationTokenSource` and check the token inside the task; throwing `OperationCanceledException` is a clean way to signal cancellation, and the task's status becomes `Canceled` rather than `Faulted`.
- **`WaitAny` has different exception semantics** (Middle): Unlike `Wait` and `WaitAll`, `WaitAny` does not propagate the first task's exception into an `AggregateException`—you must inspect task statuses after it returns, which is a common gotcha.
- **Concurrent collections are purpose-built for high concurrency** (Middle): `ConcurrentDictionary<TKey,TValue>` is the only one that allows key-based access to any item (via `TryPeek` or indexing), while `ConcurrentStack`, `ConcurrentQueue`, and `ConcurrentBag` only peek one item deep. `BlockingCollection<T>` wraps any `IProducerConsumerCollection<T>` for producer/consumer scenarios.
- **Async/await hides the complexity of state machines** (Late): The compiler generates state machines behind the scenes; understanding this helps you debug and optimize. `Task.Run` treats sync and async lambdas uniformly, while `Task.Factory.StartNew` does not—use `Unwrap` or `await` to flatten nested tasks.
【Reading Tips】
- **Skim the Q&A and "Points to Remember" boxes** (Early–Middle): These distill key insights (e.g., C# 12 collection expressions, `WaitAny` semantics) that are easy to miss in code-heavy sections.
- **Deep-read Chapter 1 for task creation and continuations** (Early): This is the bedrock; get comfortable with all five creation approaches and `TaskContinuationOptions` before moving on, as later chapters assume this knowledge.
- **Run the bank account example yourself** (Middle): The non-synchronized version produces inconsistent outputs—seeing this live makes the case for `lock` or concurrent collections much more concrete than reading about it.
- **Treat exercises as mini-labs** (Middle–Late): Exercises like E2.7 and E2.8 reveal subtle behaviors (e.g., `WaitAny` not throwing) that are easy to overlook; attempt them before checking solutions to solidify understanding.
- **Don't skip the async/await chapter's state machine section** (Late): It's the most theoretical part, but it explains why `async` methods behave the way they do—crucial for debugging real-world code.
【Coverage Limits】
This guide covers the book's core progression through Tasks, special scenarios, synchronization, concurrent collections, and async/await. It does not detail PLINQ operators or parallel loop internals in depth, as the excerpts focus on earlier chapters; readers should consult the book for those late-stage topics.
Excerpt 1
lute beginners for parallel programming. About the Author Vaskaran Sarcar obtained his Master of Engineering degree in Software Engineering from Jadavpur U...
element (without removing it) using the key. POINT TO NOTE you may note that the ConcurrentStack<T>, ConcurrentQueue<T>, and ConcurrentBag<T> classes also ...
nsuming thread. This is analogous to “streaming” the data. To exercise these options, you can use the WithMergeOptions method. This is an extension method ...
time frame. This is equivalent to asynchronous programming. The benefit is obvious: the customers do not need to stay on the call until the problem is solv...
nualResetEvent(false); 407 Appendix A SupplementAry noteS Demonstration 1 in Chapter 3 showed you an erroneous program. We corrected the program by modify...
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.
Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat PayAlipay
Open WeChat or Alipay and scan. No login required.
Add Tag
Enter tag name (max 50 characters)
Share E-Book
Parallel Programming with C and .NET Fundamentals of Concurrency and Asynchrony Behind Fast-Paced Applications (Vaskaran Sarcar)(Z-Library)
Scan QR code with your phone to access
Copy the link or scan the QR code to access this e-book on your phone
Share E-Book via Email
Please enter email address
Donation Statistics
¥.00
Total Donations
0
Donation Count
Parallel Programming with C and .NET Fundamentals of Concurrency and Asynchrony Behind Fast-Paced Applications (Vaskaran Sarcar)(Z-Library)
Find Your Favorite Books
Only registered users can comment after logging in. Comments need to be reviewed by administrators before being displayed
Loading comments...
Reply to Comment
Edit Comment