Skip to content

concurrency

8 posts with the tag “concurrency”

Work, Not Threads

Why a thread pool stops being enough, and what a work pool does instead.

A thread pool is the least surprising thing in a concurrent program, until thousands of owners share it. The twenty-fifth familiar started life as a thread pool and grew into a work pool: the same threads, but shared by owners instead of handed out by arrival, so a query never waits behind somebody else's housekeeping, and an idle pool costs nothing.

Sometimes a Mutex Is Right

A blocking channel that is not trying to be fast, only correct, with timeouts and a shutdown that actually drains.

After a chapter of lock-free structures that avoid mutexes at all costs, here is the honest counterweight: a queue that reaches straight for one. The twenty-fourth familiar is not chasing throughput. It is chasing the two things fast queues are bad at, waiting with a timeout and shutting down without losing or hanging on the items still in flight, and for those, a mutex and two condition variables are simply the right tool.

Built to Disappear

An atomic shared_ptr that becomes a one-line alias the moment the standard library grows its own.

You want to atomically swap a shared_ptr, publish a new version of some shared state without a lock of your own, and read it safely from other threads. C++20 finally standardized that, but not every compiler shipped it. The twenty-third familiar fills the gap where it is missing and, more interestingly, is written to delete itself the moment the standard library grows the feature, so the dependency has a built-in expiry date.

One Loop Per Core

The part of every TCP server you keep rewriting, written once.

Every TCP server re-implements the same three unglamorous things: a pool of event loops across the cores, the accept loop that fans connections out to them, and the shutdown dance that tears it down without hanging. None of that is your protocol, and all of it is where the concurrency bugs live. The thirteenth familiar owns exactly that and nothing else, so a new protocol is a coroutine, not a runtime.

The Line You Usually Cancel

Arming a 'this is taking too long' log on every operation, and paying only for the one that fires.

You want to know which operations are slow, but logging every operation is noise you pay for on the hot path. So you invert it: arm a 'log this if it is still running in 200 ms' line on every single operation, and cancel it, free, the instant the operation finishes, which is almost always. The seventh familiar logs the exceptions by speculating on everything, and it is the reason the sparse wheel was built at all.

The Sparse Wheel

A lock-free timer from Xapiand whose insert never stalls another thread.

One thread in Xapiand sleeps while every other thread drops timed work onto its schedule: log lines, fsyncs, commits, replication. The drop can never take a lock, and more than that, it can never stall. The little data structure that makes both true is the strangest, best thing in the whole codebase, and the familiar I would keep if I could keep only one.

The Haunted Handler

Walking every thread's stack, portably, from inside a signal handler.

Sometimes the only useful question about a stuck server is 'what is every thread doing, right now?' traceback answers it by signalling every thread at once and having each photograph its own stack. The hard part is that the stack walk is one idea spelled nine different ways across Linux, macOS, and FreeBSD, run inside a signal handler where the wrong call deadlocks the process. The second familiar out of Xapiand, written half by reasoning and half by waiting for a red build on a machine I'll never log into.