Mastering C# Concurrency

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Author(s): Eugene Agafonov; Andrew Koryavchenko
Publisher: Packt Publishing
Year: 2015

Language: English
Pages: 284

Mastering C# Concurrency
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Instant updates on new Packt books
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Traditional Concurrency
What's the problem?
Using locks
Lock statement
Monitor class
Reader-writer lock
Spin lock
Thread.SpinWait
System.Threading.SpinWait
System.Threading.SpinLock
Optimization strategy
Lock localization
Shared data minimization
Summary
2. Lock-Free Concurrency
Memory model and compiler optimizations
The System.Threading.Interlocked class
Interlocked internals
Writing lock-free code
The ABA problem
The lock-free stack
The lock-free queue
Summary
3. Understanding Parallelism Granularity
The number of threads
Using the thread pool
Understanding granularity
Choosing the coarse-grained or fine-grained approach
Summary
4. Task Parallel Library in Depth
Task composition
Tasks hierarchy
Awaiting task completion
Task cancellation
Checking a flag
Throwing an exception
Using OS wait objects with WaitHandle
Cancellation using callbacks
Latency and the coarse-grained approach with TPL
Exception handling
Using the Parallel class
Parallel.Invoke
Parallel.For and Parallel.Foreach
Understanding the task scheduler
Summary
5. C# Language Support for Asynchrony
Implementing the downloading of images from Bing
Creating a simple synchronous solution
Creating a parallel solution with Task Parallel Library
Enhancing the code with C# 5.0 built-in support for asynchrony
Simulating C# asynchronous infrastructure with iterators
Is the async keyword really needed?
Fire-and-forget tasks
Other useful TPL features
Task.Delay
Task.Yield
Implementing a custom awaitable type
Summary
6. Using Concurrent Data Structures
Standard collections and synchronization primitives
Implementing a cache with ReaderWriterLockSlim
Concurrent collections in .NET
ConcurrentDictionary
Using Lazy
Implementation details
Lock-free operations
Fine-grained lock operations
Exclusive lock operations
Using the implementation details in practice
ConcurrentBag
ConcurrentBag in practice
ConcurrentQueue
ConcurrentStack
The Producer/Consumer pattern
Custom Producer/Consumer pattern implementation
The Producer/Consumer pattern in .NET 4.0+
Summary
7. Leveraging Parallel Patterns
Concurrent idioms
Process Tasks in Completion Order
Limiting the parallelism degree
Setting a task timeout
Asynchronous patterns
Asynchronous Programming Model
Event-based Asynchronous Pattern
Task-based Asynchronous Pattern
Concurrent patterns
Parallel pipelines
Summary
8. Server-side Asynchrony
Server applications
The OWIN Web API framework
Load testing and scalability
I/O and CPU-bound tasks
Deep dive into asynchronous I/O
Real and fake asynchronous I/O operations
Synchronization context
CPU-bound tasks and queues
Summary
9. Concurrency in the User Interface
The importance of asynchrony for UI
UI threads and message loops
Common problems and solutions
How the await keyword works
Execution and synchronization contexts
Performance issues
Summary
10. Troubleshooting Parallel Programs
How troubleshooting parallel programs is different
Heisenbugs
Writing tests
Load tests
Unit tests
Integration tests
Debugging
Just my code setting
Call stack window
Threads window
Tasks window
Parallel stacks window
Performance measurement and profiling
The Concurrency Visualizer
Summary
Index