All codes were written in C++14 I am using MSVC New exercises should be added
makefile usage: (by contributer)
make #just to compile
make clean # clean the files no need after linking
make fclean # clean everything just before running make
make re # fclean and all
This repository contains concise examples showcasing core concepts and patterns in C++ multithreading. The code is organized in small, focused files for easy understanding and experimentation. Below is an explanation of what each file demonstrates:
-
000_join_and_multithreading.cpp
Demonstrates how to create a thread and use.join()
to wait for its completion. -
000_join_vs_detach.cpp
Explains the difference betweenjoin()
(blocking) anddetach()
(non-blocking). -
000_joinable.cpp
Shows how to check if a thread is joinable using.joinable()
before callingjoin()
ordetach()
.
-
001_shared_data_and_std_atomic.cpp
Introduces safe data sharing usingstd::atomic
to prevent race conditions. -
002_shared_data_and_mutexes.cpp
Shows how to protect shared data usingstd::mutex
. -
003_passing_arguments.cpp
Covers how to pass arguments to threads, including by value and by reference. -
004_lock_guard.cpp
Demonstratesstd::lock_guard
for automatic mutex locking and unlocking using RAII.
-
005_start_a_thread_with_a_function_in_a_class.cpp
Starts a thread using a member function of a class. -
005_threads_with_callable_objects.cpp
Uses functor (callable objects) to launch threads.
- 006_calculating_PI_exercise.cpp
A multithreaded example of calculating π using numerical integration.
-
007_promises_and_futures_function_args.cpp
Introducesstd::promise
andstd::future
for thread communication via function arguments. -
008_promises_and_futures_lambda_expr_for_promise.cpp
Uses lambda functions with promises and futures. -
009_promises_and_futures_with_exception.cpp
Demonstrates exception handling withstd::promise
andstd::future
. -
010_packaged_tasks.cpp
Usesstd::packaged_task
to wrap callable objects and retrieve results asynchronously. -
015_parallelism_using_async.cpp
Shows how to usestd::async
for parallel function execution with automatic future handling.
-
011_waiting_for_threads_with_a_bool.cpp
Uses a boolean flag to signal a thread to stop or wait. -
012_condition_variables.cpp
Demonstratesstd::condition_variable
for thread synchronization. -
012_two_threads_one_condition_variable.cpp
A more practical example of two threads waiting on a sharedcondition_variable
.
-
013_blocking_queue.cpp
Implements a thread-safe blocking queue using mutexes and condition variables. -
014_thread_pool.cpp
A simple thread pool implementation for managing a fixed number of worker threads. -
016_yield_vs_unyield.cpp
Compares thread yielding (std::this_thread::yield
) vs. busy-waiting.
-
999_all_the_ways_to_start_threads.cpp
A summary showcasing various ways to start a thread (function pointer, lambda, functor, etc). -
999_bonus_chunksum_async.cpp
A bonus example showing how to split a large task (like summing elements) into chunks and process them asynchronously.
- C++11 or later
- A C++ compiler like GCC, Clang, or MSVC
g++ -std=c++17 -pthread <filename.cpp> -o <output_name>
After learning launching threads and shared data mangement, you can simply follow this project
to understand more of threading usages:
https://github.com/thenaserov/cpp_multithreaded_file_organizer
Also check out log_mediator_cpp:
https://github.com/thenaserov/log_mediator_cpp