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
git clone https://github.com/Prayag2003/multithreading
cd multithreading
makemake clean./bin/00_basics_of_threads| # | Code File | Documentation | What You'll Learn |
|---|---|---|---|
| 00 | 00_basics_of_threads.cpp |
π Basics of Threads | What threads are, race conditions |
| 01 | 01_thread_creation.cpp |
π Thread Creation | Functions, functors, lambdas |
| 02 | 02_join_detach.cpp |
π join() vs detach() | Thread lifetime, waiting |
| 03 | 03_mutex.cpp |
π Mutex Protection | Protecting shared data |
| 04 | 04_try_lock.cpp |
π try_lock() | Non-blocking locks |
| 05 | 05_multiple_try_lock.cpp |
π Multiple Locks | Locking multiple mutexes safely |
| 06 | 06_recursion_lock.cpp |
π Recursive Mutex | Recursive locking |
| 07 | 07_lock_guard.cpp |
π Lock Guard | RAII, exception safety |
| 08 | 08_unique_lock.cpp |
π Unique Lock | Deferred locking, timed waits |
| 09 | 09_conditional_variable.cpp |
π Condition Variables | Thread signaling, producer-consumer pattern |
| 10 | 10_deadlock.cpp |
π Deadlock Prevention | Circular waits, prevention |
| 11 | 11_future_and_promise.cpp |
π Futures & Promises | Getting results from threads |
| 12 | 12_async.cpp |
π std::async | Automatic async execution |
| # | Code File | Documentation | What You'll Learn |
|---|---|---|---|
| 00 | 00_producer_consumer_mutex.cpp |
π Producer-Consumer (Mutex) | Bounded buffer, mutex-based synchronization |
| 01 | 01_atomic.cpp |
π Atomics | Lock-free counters, atomic operations |
| 02 | 02_binary_semaphore.cpp |
π Binary Semaphore | Semaphore basics, acquire/release pattern |
| 03 | 03_producer_consumer_semaphore.cpp |
π Producer-Consumer (Semaphore) | Elegant bounded buffer using semaphores |
| 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; });auto future = std::async(compute);
int result = future.get();std::scoped_lock lock(m1, m2);- C++20 or later
- GCC / Clang / MSVC
- pthread support
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
- 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
