#concurrency Clear
Tags #php #kotlin #bash #go #sql #rust #typescript #html #java #python #files #utils #strings #http #concurrency #async #json #arrays #security #types #crypto #database #dates #format
Go sync.Mutex vs sync.RWMutex
`Mutex` allows one goroutine at a time — for both reads and writes. `RWMutex` allows multiple concurrent readers OR one writer — much better when reads dominate.
Go select — Multiplex Channel Operations
`select` is like a switch for channels — runs whichever case is ready. Use `default` for non-blocking sends/receives; `case <-time.After(d)` for timeouts.
Go Worker Pool with Channels
Spawn N worker goroutines, feed them tasks over a jobs channel, collect results on a results channel. Idiomatic Go pattern for bounded concurrency.
Go Channels — Basics
Channels are typed pipes between goroutines. Unbuffered = synchronous handoff; buffered = up to N values queued. Close a channel to signal "no more values" — receivers see ok=false.
Go Fan-Out / Fan-In
Distribute work across N workers (fan-out), then merge their results back into one channel (fan-in). Used when you can't process items strictly in order but want to retain output as one stream.
Rust Atomic Counter (lock-free)
For shared counters and flags, `AtomicU64` / `AtomicBool` etc. are much faster than `Mutex`. Operations like `fetch_add` are single CPU instructions on most architectures.
Java Semaphore — Bounded Concurrency
Limit how many threads can enter a critical section. `acquire()` blocks until a permit is available; `release()` returns one. Use for rate limits, connection pools, or any "max N at a time" pattern.
Go errgroup — Fan-Out with Cancellation
`golang.org/x/sync/errgroup` runs N goroutines, returns the first error, and cancels the others via a shared context. The right pattern for parallel I/O where any failure should abort the rest.
TypeScript Concurrency Limiter (semaphore)
Run an array of async tasks but limit concurrency to N at a time — like p-limit, in 30 lines. Preserves input order in the output array.
Go context.Context — Cancellation & Deadlines
`context.Context` propagates deadlines, cancellation, and request-scoped values down a call chain. EVERY blocking / long-running function should take a `ctx context.Context` as its first parameter.
Go Pipeline (channel chain)
A pipeline is a chain of stages connected by channels — each stage runs in its own goroutine. Classic Go pattern for streaming transformations with backpressure built in.
Go Goroutine Leak Prevention
A goroutine that's blocked forever on a channel send/receive leaks — it's never garbage-collected. Always pair channels with `select { case <-ctx.Done(): return }` to give them an exit path.
Go sync.Pool — Reuse Allocations
`sync.Pool` keeps a pool of reusable objects to reduce allocator pressure in hot paths. Good for byte buffers, JSON encoders, or anything you allocate frequently and only need briefly.
Python asyncio Concurrency Limiter (Semaphore)
Run hundreds of coroutines but cap how many are in-flight at any moment. asyncio.Semaphore inside the task body is the cleanest way to "do at most 10 of these at a time."
Go sync.Once — Lazy One-Time Initialization
Run a function exactly once, even from many goroutines. Standard pattern for lazy singletons, expensive setup, and one-time configuration.
Java CompletableFuture — Async Chains
`CompletableFuture` is the idiomatic way to compose async work. `thenApply` for sync transforms, `thenCompose` for async chains (flatMap), `exceptionally` for fallback on failure.
Java Virtual Threads (Java 21+)
Java 21's virtual threads are millions-of-them cheap — JVM multiplexes them onto a small carrier pool. Replaces async/reactive code for most I/O-bound workloads: just write blocking code that doesn't actually block a kernel thread.
Java CountDownLatch — Wait for N Events
Block one or more threads until a count of events occurs. Set the count up front; each event calls `countDown()`; waiters call `await()`. Classic "wait until all workers are ready" pattern.