# 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](producer-consumer.md) | Decouple data production from consumption | Shared bounded queue |
| [Read-Write Lock](read-write-lock.md) | Allow concurrent reads, exclusive writes | RWMutex / RWLock |
| [Thread Pool](thread-pool.md) | Reuse a fixed set of workers | Worker pool with task queue |
| [Future / Promise](future-promise.md) | Represent a value not yet available | Async result placeholder |
| [Actor Model](actor-model.md) | Isolate state behind message-passing actors | Mailbox queues |
| [Reactor](reactor.md) | Demultiplex I/O events on a single thread | Event loop + handlers |
| [Monitor](monitor.md) | Protect shared state with mutex + conditions | Condition variables |
| [Active Object](active-object.md) | 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.
