100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

C++ Multithreading Cheat Sheet

C++ Multithreading Cheat Sheet

Covers C++ mutexes, lock guards, condition variables, atomics, and std::async/std::future for coordinating concurrent, thread-safe code.

3 PagesAdvancedApr 8, 2026

Mutex & Lock Guards

Protect shared state from concurrent access with mutexes.

cpp
#include <mutex>std::mutex mtx;int counter = 0;void increment() {    std::lock_guard<std::mutex> lock(mtx);   // RAII: unlocks automatically    ++counter;}// unique_lock allows manual unlock/relock, and is required by condition_variablestd::unique_lock<std::mutex> ulock(mtx);ulock.unlock();ulock.lock();// lock two mutexes without deadlockstd::mutex m1, m2;std::scoped_lock lock(m1, m2);   // C++17, deadlock-avoiding multi-lock

Condition Variables

Block a thread until another thread signals a condition.

cpp
#include <condition_variable>std::mutex mtx;std::condition_variable cv;bool ready = false;void worker() {    std::unique_lock<std::mutex> lock(mtx);    cv.wait(lock, [] { return ready; });   // sleeps until predicate is true    // ... do work ...}void producer() {    { std::lock_guard<std::mutex> lock(mtx); ready = true; }    cv.notify_one();   // wake one waiter; use notify_all() to wake all}

Atomics

Lock-free operations on shared variables for simple counters/flags.

cpp
#include <atomic>std::atomic<int> counter{0};counter++;                          // atomic increment, no mutex neededcounter.fetch_add(1, std::memory_order_relaxed);std::atomic<bool> flag{false};flag.store(true, std::memory_order_release);bool v = flag.load(std::memory_order_acquire);int expected = 0;counter.compare_exchange_strong(expected, 10);  // CAS operation

std::async and std::future

Run a task asynchronously and retrieve its result later.

cpp
#include <future>int compute(int x) { return x * x; }std::future<int> fut = std::async(std::launch::async, compute, 5);// ... do other work ...int result = fut.get();    // blocks until the result is ready (only callable once)std::promise<int> prom;std::future<int> f2 = prom.get_future();std::thread t([&prom] { prom.set_value(42); });t.join();std::cout << f2.get();

Synchronization Primitives

Tools available in the C++ standard library for thread coordination.

  • std::mutex- Basic mutual exclusion lock; non-recursive, must be unlocked by the locking thread.
  • std::recursive_mutex- Same thread may lock it multiple times; must unlock the same number of times.
  • std::shared_mutex- Reader-writer lock (C++17); multiple readers or one writer via lock_shared()/lock().
  • std::condition_variable- Lets threads wait for a condition and be notified when it changes.
  • std::atomic<T>- Lock-free (on most platforms) operations on a single variable without an explicit mutex.
  • Deadlock- Two or more threads waiting on locks the others hold; avoid with consistent lock ordering or std::scoped_lock.
Pro Tip

Prefer std::lock_guard or std::unique_lock over manual mutex.lock()/unlock() - an exception thrown between lock and unlock would leave the mutex permanently locked, while RAII guarantees release on stack unwind.

Was this cheat sheet helpful?

Explore Topics

#CMultithreading#CMultithreadingCheatSheet#Programming#Advanced#MutexLockGuards#ConditionVariables#Atomics#Std#Concurrency#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet