@@ -80,7 +80,7 @@ \section{Introduction to C++ threading API}
8080 \begin {itemize }
8181 \item Part of the C++11 thread support library (\texttt {<thread> }, \texttt {<mutex> }, etc.)
8282 \item Low-level, manual thread creation and management
83- \item Relies on the OS native threads under the hood
83+ \item Represents a separate thread of execution; implementations usually map it to OS threads
8484 \item Provides:
8585 \begin {itemize }
8686 \item \texttt {std::thread } for launching threads
@@ -98,7 +98,7 @@ \section{Introduction to C++ threading API}
9898 \end {itemize }
9999 Challenges and inconveniences:
100100 \begin {itemize }
101- \item non-standard APIs (towards C++)
101+ \item non-standard APIs from C++ Standard Library perspective
102102 \item platform dependent APIs
103103 \item verbose initialization boilerplate
104104 \item manual resource management
@@ -108,7 +108,8 @@ \section{Introduction to C++ threading API}
108108\begin {frame }{\texttt {std::thread } history}
109109 \begin {itemize }
110110 \item C++11 (2011): Introduced \texttt {std::thread }, \texttt {std::mutex }, \texttt {std::future }, etc.
111- \item C++14/17/20: Incremental improvements (e.g., \texttt {std::hardware\_ constructive\_ interference\_ size })
111+ \item Later standards added related tools: \texttt {std::shared\_ timed\_ mutex } (C++14),
112+ \texttt {std::shared\_ mutex } (C++17), \texttt {std::jthread }, semaphores, latches and barriers (C++20)
112113 \item Widely adopted as the base for custom thread pools and concurrency utilities
113114 \item Now the foundation of many higher-level C++ concurrency libraries
114115 \end {itemize }
@@ -141,14 +142,14 @@ \section{Introduction to C++ threading API}
141142 \textbf {Pros }
142143 \begin {itemize }
143144 \item Complete control over thread creation and destruction
144- \item No hidden scheduler—behavior is predictable
145+ \item No hidden library level task scheduler; you control (and responsible for) when threads are created and joined
145146 \item Part of standard C++, portable across platforms
146147 \item Good for learning fundamentals
147148 \end {itemize }
148149 \column {0.5\textwidth }
149150 \textbf {Cons }
150151 \begin {itemize }
151- \item Manual management—risk of leaks , detach/join errors
152+ \item Manual management: lifetime bugs, accidental \texttt { std::terminate } , detach/join errors
152153 \item Verbose boilerplate for synchronization
153154 \item No built-in task scheduling or work-stealing
154155 \item Harder to scale and tune compared to higher-level APIs
@@ -168,7 +169,7 @@ \section{C++ STL threading API (\texttt{std::thread}, \texttt{std::jthread})}
168169 \item Member functions:
169170 \begin {itemize }
170171 \item \texttt {join() }: blocks until the thread finishes execution
171- \item \texttt {detach() }: detaches the thread to run independently
172+ \item \texttt {detach() }: stops representing the thread ( detaches the thread to run independently); the thread continues without blocking the caller
172173 \item \texttt {joinable() }: checks whether the thread can be joined
173174 \item \texttt {get\_ id() }: returns the \texttt {std::thread::id } of the thread
174175 \end {itemize }
@@ -208,15 +209,15 @@ \section{C++ STL threading API (\texttt{std::thread}, \texttt{std::jthread})}
208209 \end {lstlisting }
209210 \begin {itemize }
210211 \item Construct \texttt {std::thread } with a callable + optional args
211- \item Must call \texttt {join() } or \texttt {detach() } before destruction
212+ \item Must call \texttt {join() } or \texttt {detach() } before destruction; otherwise the destructor calls \texttt { std::terminate }
212213 \item Threads are move-only: no copy construction/assignment
213214 \end {itemize }
214215\end {frame }
215216
216217\begin {frame }[fragile]{Managing thread lifetime}
217218 \begin {itemize }
218219 \item \texttt {join() }: waits for thread completion
219- \item \texttt {detach() }: allows thread to run independently (daemon-like)
220+ \item \texttt {detach() }: allows the thread to continue independently until it finishes or the program terminates
220221 \end {itemize }
221222 \lstset {style=CStyle}
222223 \ begin{lstlisting}
@@ -230,7 +231,7 @@ \section{C++ STL threading API (\texttt{std::thread}, \texttt{std::jthread})}
230231 \item \texttt {joinable() }: check if thread can be joined
231232 \end {itemize }
232233 \begin {itemize }
233- \item Detached threads continue after \texttt {main }—dangerous if resources go out of scope
234+ \item Detached threads are not joined at \texttt {main } exit and may outlive objects they access
234235 \item Always ensure each thread is either joined or detached
235236 \end {itemize }
236237\end {frame }
@@ -272,7 +273,7 @@ \section{C++ STL threading API (\texttt{std::thread}, \texttt{std::jthread})}
272273\begin {frame }[fragile]{\texttt {std::jthread }}
273274 \begin {itemize }
274275 \item Introduced in C++20 as a safer and more convenient alternative to \texttt {std::thread }.
275- \item Automatically joins upon destruction, reducing the risk of detached threads .
276+ \item On destruction, requests stop and then joins if it still represents a thread .
276277 \item Supports cooperative cancellation via \texttt {std::stop\_ token }.
277278 \end {itemize }
278279 Example usage:
@@ -306,7 +307,7 @@ \section{\texttt{std::future}, \texttt{std::promise} and \texttt{std::async}}
306307 \item Future and promise provide a mechanism for asynchronous communication between threads
307308 \item A promise allows one thread to set a value or report an error
308309 \item The associated future retrieves the value, waiting if necessary
309- \item std::async simplifies launching asynchronous tasks without explicit thread management
310+ \item \texttt { std::async } simplifies launching asynchronous tasks without explicit thread management
310311 \item These features promote decoupling of computation and enhance concurrency control
311312 \end {itemize }
312313\end {frame }
@@ -316,6 +317,7 @@ \section{\texttt{std::future}, \texttt{std::promise} and \texttt{std::async}}
316317 \ begin{lstlisting}
317318#include <future>
318319#include <iostream>
320+ #include <thread>
319321
320322int compute() {
321323 int result;
@@ -337,21 +339,23 @@ \section{\texttt{std::future}, \texttt{std::promise} and \texttt{std::async}}
337339}
338340 \end {lstlisting }
339341
340- \texttt {std::promise } sets a value ( or failure)
342+ \texttt {std::promise } sets a value or exception
341343
342344 \texttt {std::future } retrieves it on demand
343345\end {frame }
344346
345347\begin {frame }[fragile]{Using \texttt {std::async }}
346348 \begin {itemize }
347- \item Launches tasks asynchronously and returns a \texttt {std::future }
348- \item Can run immediately in a new thread or be deferred until needed
349+ \item Launches a task and returns a \texttt {std::future }
350+ \item With default policy, may run in a new thread or be deferred until a waiting function is called
349351 \item Simplifies asynchronous programming by handling thread management
350352 \end {itemize }
351353 \lstset {style=CStyle}
352354 \ begin{lstlisting}
353355#include <future>
354356#include <iostream>
357+ #include <thread>
358+ #include <chrono>
355359
356360int computeSquare(int x) {
357361 // Simulate heavy computation
@@ -376,7 +380,7 @@ \section{\texttt{std::future}, \texttt{std::promise} and \texttt{std::async}}
376380int computeCube(int x) { ... }
377381
378382int main() {
379- // Using deferred launch: execution is postponed until get() is called
383+ // Using deferred launch: execution is postponed until get() or wait() is called
380384 std::future<int> futDeferred = std::async(std::launch::deferred, computeCube, 3);
381385 std::cout << "Deferred result: " << futDeferred.get() << std::endl;
382386
@@ -542,21 +546,42 @@ \section{Synchronization primitives (mutexes, condition variables, \dots)}
542546 \texttt {std::atomic } provides lock-free operations. Use \texttt {fetch\_ add } method and load to operate on atomic variables safely
543547\end {frame }
544548
549+ \begin {frame }{Other synchronization primitives}
550+ \begin {itemize }
551+ \item Mutex variants:
552+ \begin {itemize }
553+ \item \texttt {std::timed\_ mutex }: lock with timeout or deadline
554+ \item \texttt {std::recursive\_ mutex }: same thread may lock multiple times
555+ \item \texttt {std::shared\_ mutex }: many readers or one writer
556+ \end {itemize }
557+ \item One-time initialization:
558+ \begin {itemize }
559+ \item \texttt {std::once\_ flag } and \texttt {std::call\_ once }
560+ \end {itemize }
561+ \item C++20 coordination primitives:
562+ \begin {itemize }
563+ \item \texttt {std::counting\_ semaphore }, \texttt {std::binary\_ semaphore }: permit-based access
564+ \item \texttt {std::latch }: one-shot countdown
565+ \item \texttt {std::barrier }: reusable phase synchronization
566+ \end {itemize }
567+ \end {itemize }
568+ \end {frame }
569+
545570\section {Best practices and recommendations }
546571
547572\begin {frame }{Best practices and recommendations}
548573 \begin {itemize }
549- \item Always join or detach threads before destruction
550- \item Prefer RAII wrappers ( \texttt {std::lock \_ guard }, \texttt { std::unique \_ lock })
551- \item Minimize shared state; prefer message passing or futures
552- \item Be cautious with detached threads, manage lifetimes carefully
553- \item Consider higher-level thread pools (e.g., \texttt { std::async })
574+ \item Always join or detach \texttt { std::thread } objects before destruction; otherwise \texttt { std::terminate } is called
575+ \item Prefer \texttt {std::jthread } in C++20 when automatic stop request and join behavior is appropriate
576+ \item Prefer RAII locking wrappers and keep critical sections small
577+ \item Minimize shared state; prefer futures, promises, \texttt { std::async }
578+ \item Be cautious with detached threads; make ownership and lifetimes explicit
554579 \end {itemize }
555580\end {frame }
556581
557582\begin {frame }{When and where to use \texttt {std::thread } in real world scenarios}
558583 \begin {itemize }
559- \item Fine-grained control over threading
584+ \item Fine-grained control over thread lifetime and execution
560585 \item Building custom schedulers or thread pools
561586 \item Interfacing directly with OS thread APIs
562587 \item Performance critical sections where you avoid scheduler overhead
0 commit comments