Skip to content

Prayag2003/multithreading

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C++ Threading & Synchronization Learning Guide 🧵

A beginner-friendly learning repository for C++ concurrency concepts. Learn step-by-step with clear examples and explanations.

🔗 Resources

📚 What You'll Learn

  • Fundamentals: Core threading concepts from scratch
  • Implementations: Real-world patterns and solutions
  • Best Practices: How to write safe, efficient concurrent code

🚀 Quick Start

Build All Examples (using Makefile)

git clone https://github.com/Prayag2003/multithreading
cd multithreading
make

Output:

alt text

Clean all built binaries

make clean

Run an Example

./bin/00_basics_of_threads

📖 Fundamentals: Threading Basics

# 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

🏗️ Implementations: Real-World Patterns

# 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

🎯 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

💡 Common Patterns

Protect Shared Data

std::mutex mtx;
std::lock_guard<std::mutex> guard(mtx);

Wait for a Signal

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);

🔧 Requirements

  • 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

  1. Start with Fundamentals → Work through 00 → 12
  2. Read Documentation → Understand each concept deeply
  3. Run Examples → See how they work
  4. Experiment → Modify values, add threads, cause bugs
  5. Move to Implementations → Learn real-world concurrency patterns