agentleFS
13eholder13eholder/Modern-Cpp-Skills/m07-concurrency/SKILL.md

m07-concurrency

Skill14 starsChanged 9 months agoMITView on GitHub

Mastering C++ Concurrency. Triggers: std::thread, jthread, atomic, mutex, deadlock, race condition, memory model, coroutine, async.

7 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Files
SKILL.md

C++ Concurrency

Core Question

How do threads communicate?

  • Shared State: Mutexes (std::mutex) or Atomics (std::atomic).
  • Coordination: Condition Variables (std::condition_variable) or Latches/Semaphores (C++20).
  • Tasks: std::async or Coroutines (C++20).

Error → Design Question

IssueDesign Question
Data RaceAre two threads accessing memory without a Happens-Before edge?
DeadlockDid you lock Mutex A then B, while another thread locked B then A?
False SharingAre independent atomics sitting on the same cache line?
Live LockAre threads spinning without progress?

Thinking Prompt

  1. Do I need a thread?
    • Short task? → std::async or Thread Pool.
    • Long background? → std::jthread.
  2. Is it shared state?
    • Yes? → Protect with std::mutex.
    • Is it small/primitive? → std::atomic.
  3. Locks or Atomics?
    • Logic requiring multiple steps? → Mutex (Atomic is only for single instructions).
    • Simple flag/counter? → Atomic.

Trace Up / Down

  • Trace Up:
    • Issue: "Random update values in counter."
    • Cause: counter++ is Read-Modify-Write, not atomic. Data race.
    • Fix: std::atomic<int>.

Quick Reference

ToolC++ VersionUse When
std::jthreadC++20Standard thread (auto-join).
std::atomicC++11Lock-free counters/flags.
std::mutexC++11Locking critical sections.
std::shared_mutexC++17Read-heavy workloads.
std::latchC++20Waiting for N tasks to start.
co_awaitC++20Async I/O (requires library).

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.