clean-code-concurrency

Installation
SKILL.md

Concurrency

Concurrency is a decoupling strategy: it separates what gets done from when. That can improve throughput and structure, but concurrent code is hard to get right. Code that's simple to follow single-threaded can become a nightmare once multiple threads and shared data are involved.

Myths to correct

  • Concurrency always improves performance. It helps only when there's a lot of wait time (such as I/O) that can be overlapped, or real parallel work across cores.
  • Design doesn't change. Decoupling what from when usually changes the structure of the system considerably.
  • The container or framework handles it. You need to know what your web server, framework or runtime is doing about concurrency and how to guard against its issues.

More balanced truths: concurrency adds overhead in performance and code, correct concurrency is complex even for simple problems, its bugs are rarely repeatable and so get dismissed as one-offs, and it often requires a fundamental change in design strategy.

Why it's hard

Even return ++last_id isn't atomic. It compiles to several low-level steps, and two threads can interleave them to return the same id, or skip one. The number of possible interleavings explodes with code size. Most of them are fine, which is exactly why the broken ones hide. See concurrencyDeepDive.md for the worked analysis.

Defense principles

Installs
1
First Seen
2 days ago