This repository is dedicated to studying concurrency in C++ based on the textbook "C++ Concurrency in Action: Practical Multithreading" (1st Edition) by Anthony Williams. The goal is to dive deep into modern C++ multithreading and parallel programming concepts, exploring real-world examples and practice exercises.
This repository provides a hands-on approach to learning C++ concurrency, following the key topics and examples from the textbook "C++ Concurrency in Action". Each section corresponds to a chapter in the book and includes code examples, exercises, and notes for further study.
- Read the book: Start by reading the corresponding chapters in C++ Concurrency in Action.
- Follow the code: Each chapter has a folder with code examples that align with the topics covered. Try running and modifying these examples to deepen your understanding.
- Do the exercises: In addition to book exercises, this repo includes extra challenges for you to practice C++ concurrency.
- Contribute: Feel free to submit pull requests with your own examples, improvements, or alternative solutions.
- Basics of C++ threads and multithreading.
- Creating and managing threads using
std::thread. - Simple "Hello, World" concurrent program.
Example Code:
chapter1/hello_world.cpp
- Thread lifecycle management.
- Detaching and joining threads.
- RAII for thread management using
std::thread.
Example Code:
chapter2/thread_management.cpp
- Sharing data safely between threads.
- Using mutexes (
std::mutex), locks (std::lock_guard), and unique locks (std::unique_lock). - Thread-safe data structures.
Example Code:
chapter3/sharing_data.cpp
- Synchronization techniques using condition variables (
std::condition_variable). - Avoiding race conditions and deadlocks.
- Designing concurrent algorithms.
Example Code:
chapter4/synchronization.cpp
- Introduction to the C++ memory model.
- Atomic operations and
std::atomic. - Memory ordering and synchronization guarantees.
Example Code:
chapter5/atomic_operations.cpp
[...]
-
Requirements:
- C++11 or later compiler (e.g., g++, clang++)
- CMake for building the examples
-
Building:
To build the code, use the following commands:mkdir build cd build cmake .. make -
Running:
After building, run the examples by executing the compiled binaries:./chapter1/hello_world