Synchronization PatternsThread Signaling & CoordinationMedium⏱️ ~2 min

The Multiplex Pattern

Definition
A multiplex allows up to N threads into a critical section simultaneously. It is like a mutex that permits multiple holders instead of just one.

Beyond Mutual Exclusion

A mutex allows exactly 1 thread. But what if a resource can handle multiple concurrent users, just not unlimited? A database connection pool has 10 connections. A license server allows 5 concurrent users. You need controlled concurrency, not full exclusion.

The Nightclub Bouncer

A popular club has a fire code limit of 100 people. The bouncer counts. When someone enters, count goes up. When someone leaves, count goes down. At 100, new arrivals wait outside. When one person exits, one waiting person enters.

Multiplex: N Threads Allowed
Critical SectionN=3max concurrentT1T2T3T4T5waiting...

The Elegant Solution

Initialize a semaphore to N (not 1). Each thread calls wait() before entering - this decrements the count. Each thread calls signal() when leaving - this increments the count. When count reaches 0, new threads block.

The semaphore value means: "How many more threads can enter right now?" If it is 3, three more can enter. If it is 0, the next arrival blocks until someone leaves.

Mutex Is Just Multiplex Where N=1

A mutex is a special case. Initialize the semaphore to 1. Only one thread can hold it. Multiplex generalizes this to any capacity.

Real-World Use: Connection pools, rate limiters, license servers, and thread pools all use the multiplex pattern. It bounds concurrency to protect limited resources.
💡 Key Takeaways
Multiplex allows N threads in a critical section. It is a generalized mutex with configurable capacity.
Initialize semaphore to N. wait() decrements, signal() increments. When value hits 0, threads block.
The semaphore value represents available slots. Positive means room available. Zero means wait.
A mutex is multiplex with N=1. Multiplex is the general case for bounded concurrency.
Use for connection pools, license servers, rate limiters - anywhere you need to limit concurrent access to a resource.
📌 Examples
1Database pool: 10 connections available. Semaphore initialized to 10. Thread acquires connection (wait), uses it, returns (signal).
2API rate limiter: Allow 100 concurrent requests. Request 101 blocks until one of the first 100 completes.
3License server: Software allows 5 concurrent users. User 6 waits until someone logs out.
← Back to Thread Signaling & Coordination Overview
The Multiplex Pattern | Thread Signaling & Coordination - System Overflow