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