Files

Concurrency Patterns

Overview

Concurrency patterns address the challenges of executing multiple computations simultaneously, managing shared resources, synchronizing threads, and structuring asynchronous workflows. These patterns are essential for building responsive, high-throughput systems.

Patterns in This Category

Pattern Purpose Key Mechanism
Producer-Consumer Decouple data production from consumption Shared bounded queue
Read-Write Lock Allow concurrent reads, exclusive writes RWMutex / RWLock
Thread Pool Reuse a fixed set of workers Worker pool with task queue
Future / Promise Represent a value not yet available Async result placeholder
Actor Model Isolate state behind message-passing actors Mailbox queues
Reactor Demultiplex I/O events on a single thread Event loop + handlers
Monitor Protect shared state with mutex + conditions Condition variables
Active Object Decouple method invocation from execution Request queue + scheduler

When to Use Concurrency Patterns

  • I/O-bound workloads: Reactor, Future/Promise, Actor Model
  • CPU-bound workloads: Thread Pool, Producer-Consumer
  • Shared mutable state: Monitor, Read-Write Lock
  • Message-driven architectures: Actor Model, Active Object
  • Event-driven systems: Reactor

General Principles

  1. Prefer message passing over shared memory when possible.
  2. Minimize critical sections to reduce contention.
  3. Avoid nested locks to prevent deadlocks.
  4. Use bounded queues to apply backpressure.
  5. Design for cancellation and timeout in all async operations.