C++ Threading & Synchronization Learning Guide 🧵
A beginner-friendly learning repository for C++ concurrency concepts. Learn step-by-step with clear examples and explanations.
Fundamentals : Core threading concepts from scratch
Implementations : Real-world patterns and solutions
Best Practices : How to write safe, efficient concurrent code
Build All Examples (using Makefile)
git clone https://github.com/Prayag2003/multithreading
cd multithreading
make
./bin/00_basics_of_threads
📖 Fundamentals: Threading Basics
🏗️ Implementations: Real-World Patterns
🎯 Key Concepts Quick Reference
Concept
Use When
See Example
Mutex
Protecting shared data
03_mutex.cpp
Lock Guard
Simple RAII locking
07_lock_guard.cpp
Unique Lock
Manual lock control
08_unique_lock.cpp
Condition Variables
Thread coordination
09_conditional_variable.cpp
std::async
Fetching results from threads
12_async.cpp
Atomics
Lock-free shared counters/flags
01_atomic.cpp
Semaphores
Resource counting
03_producer_consumer_semaphore.cpp
std::mutex mtx;
std::lock_guard<std::mutex> guard (mtx);
cv.wait(lock, [] { return ready; });
Get a Result from a Thread
auto future = std::async(compute);
int result = future.get();
Lock Multiple Resources Safely
std::scoped_lock lock (m1, m2);
C++20 or later
GCC / Clang / MSVC
pthread support
📚 Project Structure (UPDATED)
threading-guide/
├── fundamentals/
│ ├── *.cpp # Basic threading examples
│ └── documentation/ # Markdown docs for fundamentals
│ └── *.md
├── implementations/
│ ├── *.cpp # Advanced real-world patterns
│ └── documentation/ # Markdown docs for implementations
│ └── *.md
├── bin/ # Auto-created binaries (via Makefile)
├── Makefile
└── README.md
🎓 How to Use This Repository
Start with Fundamentals → Work through 00 → 12
Read Documentation → Understand each concept deeply
Run Examples → See how they work
Experiment → Modify values, add threads, cause bugs
Move to Implementations → Learn real-world concurrency patterns