forked from hsf-training/cpluspluscourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadlocal.tex
27 lines (26 loc) · 863 Bytes
/
threadlocal.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
\subsection[TLS]{Thread-local storage}
\begin{frame}[fragile]
\frametitlecpp[20]{Thread-local storage}
\begin{block}{\cppinline{thread_local} keyword}
\begin{itemize}
\item A variable can be declared \cppinline{thread-local}
\item Then every thread will have its own copy
\item Most useful for ``working memory'' in each thread
\item Note: Can also be declared directly on the stack of a thread
\begin{itemize}
\item and will be faster, thus should be preferred when possible
\end{itemize}
\end{itemize}
\end{block}
\begin{exampleblock}{}
\begin{cppcode*}{}
thread_local int a{0};
{
std::jthread t1([&] { a++; });
std::jthread t2([&] { a++; });
a += 2;
}
assert( a == 2 ); // Guaranteed to succeed
\end{cppcode*}
\end{exampleblock}
\end{frame}