This repository provides examples and exercises on parallel programming in .NET, covering synchronization mechanisms, concurrent collections, task coordination, parallel loops, and Parallel LINQ.
-
Main Topics Learned
-
Concurrency != Parallelism:
-
Concurrency: It's the capacity to handle multiple tasks at once
-
Parallelism: It's the capacity to execute multiple tasks at once
-
-
Concurrency Synchronization: Covers different synchronization mechanisms to manage concurrent access to shared resources
-
Concurrent Collections: Covers the usage of thread-safe collections
-
Task Coordination: Covers different techniques to coordinate tasks
-
Parallel Loops: Covers the use of parallel loops to perform parallel operations on collections
-
Parallel LINQ: Covers the usage of PLINQ (Parallel LINQ) to perform parallel operations on collections
-
-
Topics in Details
-
Concurrency Synchronization
-
LockSync: Demonstrates the use of
lockstatement to synchronize access to a shared resource. -
Interlocked: Shows how to use
Interlockedclass for atomic operations on shared variables. -
SpinLockSync: Explains the use of
SpinLockfor low-level synchronization. -
MutexSync: Demonstrates the use of
Mutexfor inter-process synchronization. -
ReaderWriterLockSync: Shows how to use
ReaderWriterLockSlimto allow multiple readers or exclusive access to a resource.
-
-
Concurrent Collections
-
ListAndConcurrentBag: Compares the use of
Listwith locks andConcurrentBagfor thread-safe operations. -
DictionaryAndConcurrentDictionary: Compares the use of
Dictionarywith locks andConcurrentDictionaryfor thread-safe operations. -
QueueAndConcurrentQueue: Compares the use of
Queuewith locks andConcurrentQueuefor thread-safe operations. -
StackAndConcurrentStack: Compares the use of
Stackwith locks andConcurrentStackfor thread-safe operations.
-
-
Task Coordination
-
Task Continuation: Demonstrates how to chain tasks using continuations.
-
Child Tasks: Shows how to create and manage child tasks.
-
Barrier: Explains the use of
Barrierto synchronize multiple tasks at a specific point. -
Countdown Event: Demonstrates the use of
CountdownEventto wait for multiple tasks to signal completion. -
Reset Event: Shows how to use
ManualResetEventSlimfor signaling between tasks. -
Semaphore: Demonstrates the use of
SemaphoreSlimto limit the number of concurrent tasks.
-
-
Parallel Loops
-
Parallel Invoke: Demonstrates the use of
Parallel.Invoketo run multiple actions in parallel. -
ForEach and ForEachAsync: Shows how to use
Parallel.ForEachandParallel.ForEachAsyncfor parallel iteration over collections. -
For and ForAsync: Demonstrates the use of
Parallel.ForandParallel.ForAsyncfor parallel iteration with indices. -
Thread Local Storage: Explains the use of thread-local storage in parallel loops.
-
Partitioning: Shows how to partition data for parallel processing.
-
Handling Exceptions: Demonstrates how to handle exceptions in parallel loops.
-
-
Parallel LINQ
-
AsParallel: Demonstrates the use of
AsParallelto enable parallel processing of LINQ queries. -
ParallelQuery: Shows how to create and use
ParallelQueryfor parallel LINQ operations. -
Handling Cancellations and Exceptions: Explains how to handle cancellations and exceptions in PLINQ queries.
-
Merge Options: Demonstrates the use of merge options to control the buffering behavior of PLINQ queries.
-
Custom Aggregations: Shows how to perform custom aggregations in PLINQ queries.
-
-