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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
\input{configpres}
\def\lximg{/usr/share/lx/icons/fueller.png}
\subsection{C++ Threads}
\title{Threading in C++}
\maketitle
% stop displaying 'fueller.png' on the following slides
\def\lximg{none}
%\begin{frame}
%\tableofcontents
%\end{frame}
\subsubsection{Threading}
\begin{frame}
\frametitle{Threads in C++}
\begin{itemize}
\item Platform independet concurrency available since C++11
\item Prior to C++11: platform-specific extensions like OpenMP, PThreads
\item Classes for thread management, manipulation and synchronisation
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{C++ Thread Management}
Creating and starting a thread:
\begin{lstlisting}
#include <iostream>
#include <thread>
void thread_fn()
{
std::cout << "This output was generated in a thread." << std::endl;
}
int main()
{
std::thread my_thread(thread_fn); // instantiate thread object
my_thread.join(); // wait for my_thread to finish
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{C++ Thread Management}
Passing parameters to a thread function:
\begin{lstlisting}
#include <iostream>
#include <thread>
void thread_add(int a, int b)
{
int c = a + b;
std::cout << "Addition result: " << c << std::endl;
}
int main()
{
std::thread my_thread_add(thread_add, 10, 20);
my_thread_add.join(); // wait for addition to finish
}
\end{lstlisting}
\end{frame}
\subsubsection{Synchronisation}
\begin{frame}
\frametitle{Synchronisation Mechanisms}
\begin{itemize}
\item Synchronisation of threads is mandatory
\item Basic synchronisation primitives:
\begin{itemize}
\item mutex objects
\item condition variables
\end{itemize}
\item Advanced synchronisation primitives:
\begin{itemize}
\item futures
\item atomics
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Mutex}
Using mutex objects:
\begin{lstlisting}
#include <mutex>
std::mutex my_mutex;
unsigned int up_counter = 0;
unsigned int counter_inc()
{
std::lock_guard<std::mutex> lock(my_mutex);
return ++up_counter;
}
unsigned int counter_read()
{
std::lock_guard<std::mutex> lock(my_mutex);
return up_counter;
}
\end{lstlisting}
Using direct calls to the methods lock() and unlock() is possible too.
We must ensure to unlock the mutex at each exit from protected regions
including the launch of exceptions. So it's more safe to to use the
lockguard template!
\end{frame}
\begin{frame}[fragile]
\frametitle{Condition variables}
Using condition variables:
\begin{lstlisting}
#include <condition_variable>
std::mutex m; std::condition_variable cv; bool processed = false;
void thread_fn()
{
processed = true;
std::cout << "Thread signals data processing completed\n";
cv.notify_one();
}
int main()
{
std::thread my_thread(thread_fn);
std::unique_lock<std::mutex> lk(m);
cv.wait(lk, []{return processed;});
if(processed)
std::cout << "Main thread got the signal\n";
else
std::cout << "Oops, something went wrong\n";
my_thread.join();
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Futures}
Futures are used to launch asynchronous operations whose results are
not required immediately.
\begin{lstlisting}
#include <future>
int long_time_computation()
{
int a = 5, b = 3;
return a + b;
}
void do_other_stuff()
{
std::cout << "We print this while long_time_computation is ongoing\n";
}
int main()
{
std::future<int> the_result = std::async(long_time_computation);
do_other_stuff();
std::cout << "The result is " << the_result.get() << std::endl;
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Atomics}
\begin{verbatim}
#include <atomic>
std::atomic<int> counter = 1;
counter++;
\end{verbatim}
\begin{itemize}
\item An atomic type can be used with any template T type
\item Operations on atomics will be executed in its entirety or not at all
\item Good performance due to lock free implementation
\end{itemize}
\end{frame}
\subsubsection{C++-Threads going realtime}
\begin{frame}[fragile]
\frametitle{C++-Threads going realtime}
C++-Threads can be brought to realtime context using the PThread-API
\begin{lstlisting}
#include <thread>
#include <pthread.h>
int main()
{
std::thread my_thread;
sched_param sch;
int policy;
pthread_getschedparam(my_thread.native_handle(), &policy, &sch);
sch.sched_priority = 20;
if(pthread_setschedparam(my_thread.native_handle(), SCHED_FIFO, &sch))
{
std::cout << "Failure: " << std::strerror(errno) << std::endl;
}
}
\end{lstlisting}
\end{frame}
\input{tailpres}
|