Why Concurrency is Hard
Concurrency introduces non-determinism. Without proper synchronization, you get race conditions, deadlocks, and starvation.
This guide covers the fundamental patterns used to serialize access to shared resources.
1. Mutex (Mutual Exclusion)
The simplest synchronization primitive. Only one thread can hold the lock at a time.
Use Case: Protecting a shared counter or critical section.
package main
import (
"sync"
)
type SafeCounter struct {
mu sync.Mutex
v map[string]int
}
func (c *SafeCounter) Inc(key string) {
c.mu.Lock() // Lock before accessing map
defer c.mu.Unlock() // Ensure unlock happens even if panic
c.v[key]++
}
[!WARNING] Forget to unlock? Deadlock. Using
deferin Go ortry-finallyin Java/Python is critical.
2. Semaphore
A mutex that allows concurrent accesses.
Use Case: Limiting connections to a database or rate limiting.
graph TD
Queue[Threads Waiting] -->|Acquire| Sem{Semaphore N=3}
Sem -->|Permit 1| Res1[Resource 1]
Sem -->|Permit 2| Res2[Resource 2]
Sem -->|Permit 3| Res3[Resource 3]
Res1 -->|Release| Sem
3. Read-Write Lock (RWMutex)
Allows multiple readers OR one writer.
Use Case: Caches where reads vastly outnumber writes.
- RLock(): Blocks if a writer holds lock. Allows other readers.
- Lock(): Blocks until all readers finish. Exclusive access.
4. Monitor Pattern
Combines a Mutex with Condition Variables (Wait/Notify).
Pattern:
- Acquire Lock.
- Check Condition. If false, Wait (release lock, sleep).
- Do work.
- Notify waiting threads.
- Release Lock.
Java Example (Synchronized):
public synchronized void produce(Item item) {
while (queue.isFull()) {
wait(); // Releases lock and sleeps
}
queue.add(item);
notifyAll(); // Wakes up consumers
}
5. Communicating Sequential Processes (CSP)
Popularized by Go. "Do not communicate by sharing memory; share memory by communicating."
Key Idea: Use Channels to pass data between Goroutines. No explicit locks.
package main
func main() {
jobs := make(chan int, 100)
results := make(chan int, 100)
// Start 3 workers
for w := 1; w <= 3; w++ {
go worker(w, jobs, results)
}
// Send jobs
for j := 1; j <= 5; j++ {
jobs <- j
}
close(jobs)
}
func worker(id int, jobs <-chan int, results chan<- int) {
for j := range jobs {
results <- j * 2
}
}
6. Actor Model
Popularized by Erlang and Akka (Scala/Java).
Key Concepts:
- Actor: Fundamental unit of computation.
- Mailbox: Actors communicate only by sending messages.
- State: Private, never shared. Modified only by processing messages.
- No Locks: Messages are processed sequentially.
sequenceDiagram
participant A as Actor A
participant B as Actor B
A->>B: Send Message "Compute(5)"
Note right of B: Add to Mailbox
B->>B: Process "Compute(5)"
B->>A: Send Result "25"
Comparison
| Pattern | Shared State? | Locking? | Best For |
|---|---|---|---|
| Mutex | Yes | Explicit | Fine-grained state protection |
| CSP (Channels) | No | Implicit | Stream processing, Pipelines |
| Actor Model | No | None | Distributed systems, Fault tolerance |
| Atomics | Yes | Hardware | Counters, Flags (High Perf) |
Related Concepts
- Process vs Thread
- Distributed Transactions (Sagas are like distributed Monitors)
- Event Sourcing
About ScaleWiki
ScaleWiki is an interactive educational platform dedicated to demystifying distributed systems, software architecture, and system design. Our mission is to provide high-quality, technically accurate resources for software engineers preparing for interviews or solving complex scaling challenges in production.
Read more about our Editorial Guidelines & Authorship.
Educational Disclaimer: The architectural patterns and system designs discussed in this article are based on common industry practices, technical whitepapers, and public engineering blogs. Actual implementations in enterprise environments may vary significantly based on specific product requirements, legacy constraints, and evolving technologies.
Related Articles
Process vs Thread
Understanding the fundamental units of execution in operating systems. A deep dive into memory models, context switching costs, and concurrency implementation details.
Docker Internals
What actually is a container? Just a Linux process with a mask on. Deep dive into Namespaces, Cgroups, and Union Filesystems (OverlayFS).
Event Sourcing & CQRS
Comprehensive guide to Event Sourcing and Command Query Responsibility Segregation (CQRS) patterns, covering immutable event logs, state reconstruction, read/write separation, and real-world implementations in banking, e-commerce, and audit systems.