-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4.html
More file actions
341 lines (341 loc) · 11.6 KB
/
day4.html
File metadata and controls
341 lines (341 loc) · 11.6 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<!DOCTYPE HTML>
<html lang="de">
<head>
<title>Programming C#</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=1274, user-scalable=no">
<link rel="stylesheet" href="themes/styles/style.css">
</head>
<body class="list">
<header class="caption">
<h1>Programming C#</h1>
</header>
<div class="slide cover" id="Cover"><div>
<section>
<header>
<h2>Programming C#</h2>
<h3>Day 4: Threads and tasks, TPL, await and async and user controls</h3>
</header>
<img src="pictures/cover.jpg" alt="">
</section>
</div></div>
<div class="slide" id="overview"><div>
<section>
<header>
<h2>Content</h2>
</header>
<ul>
<li>Sets and LINQ</li>
<li>The message loop, threads and GUI synchronization</li>
<li>Wrapping threads in tasks</li>
<li>The Task Parallel Library (TPL)</li>
<li>Considerations for multi-threading</li>
<li>Using the asynchronous keywords</li>
</ul>
</section>
</div></div>
<div class="slide" id="sets-linq"><div>
<section>
<header>
<h2>Sets and LINQ</h2>
</header>
<ul>
<li>Reminder: LINQ execution is deferred!</li>
<li>LINQ is specialized on handled collections</li>
<li>Collections can be quite similar to sets</li>
<li>There are several methods for doing set math</li>
<li>Unions, intersections, exceptions, ...</li>
<li>Quite important is certainly <code>Distinct()</code></li>
</ul>
</section>
</div></div>
<div class="slide" id="venn-linq"><div>
<section>
<header>
<h2>Venn diagrams</h2>
</header>
<img src="pictures/venn.png" class="middle r" />
</section>
</div></div>
<div class="slide" id="example-linq"><div>
<section>
<header>
<h2 class="example">→ Example - Sets and LINQ</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day4/LinqSets.cs" title="Download source" class="example-download">LinqSets.cs</a>
</section>
</div></div>
<div class="slide" id="message-loop"><div>
<section>
<header>
<h2>The message loop</h2>
</header>
<ul>
<li>How does a GUI work? It's a big loop</li>
<li>This is called the message loop</li>
<li>The OS queues messages for our app</li>
<li>These messages get pumped once the app is idle</li>
<li>Ergo the idle state of an app is very important</li>
<li>Doing too much work will result in a non-responsive app</li>
</ul>
</section>
</div></div>
<div class="slide" id="threads"><div>
<section>
<header>
<h2>The cure: threads</h2>
</header>
<ul>
<li>Hence the OS has a model called: threads</li>
<li>Every app comes with 1 thread: the GUI thread</li>
<li>Advantage with several (CPU) cores: prob. faster execution</li>
<li>Advantage even with just a single core: responsive UI</li>
<li>The reason is that the OS schedules CPU time</li>
<li>The only question is now: How can we create threads?</li>
</ul>
</section>
</div></div>
<div class="slide" id="creating-threads"><div>
<section>
<header>
<h2>Creating threads</h2>
</header>
<ul>
<li>We need a method that should run in that thread</li>
<li>The class <code>Thread</code> represents a thread</li>
<li>The constructor requires a delegate of the method to run</li>
<li>The namespace <code>System.Threading</code> contains the class and more</li>
<li>Running <code>Start()</code> will run the method in another thread</li>
<li>Important: Exceptions from other threads will bubble up!</li>
</ul>
</section>
</div></div>
<div class="slide" id="example-threads"><div>
<section>
<header>
<h2 class="example">→ Example - Threads</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day4/Threads.cs" title="Download source" class="example-download">Threads.cs</a>
</section>
</div></div>
<div class="slide" id="thread-problems"><div>
<section>
<header>
<h2>Problems with threads</h2>
</header>
<ul>
<li>Threads are running concurrently</li>
<li>Spawning multiple threads results in overhead</li>
<li>Consider using the <code>ThreadPool</code> for many threads</li>
<li>Avoid race conditions, i.e. solving non-independent problems</li>
<li>Biggest problem: How to communicate between threads?</li>
<li>Another problem: Changing the UI is not possible</li>
</ul>
</section>
</div></div>
<div class="slide" id="lock-thread"><div>
<section>
<header>
<h2>Thread barriers</h2>
</header>
<ul>
<li>C# introduces the <code>lock</code> keyword</li>
<li>The <code>lock</code> blocks usage on certain lines of code</li>
<li>Barriers help to reduce race conditions</li>
<li>The barrier status (set / unset) is determined by a pointer</li>
<li>A pointer is here just given by a reference type</li>
</ul>
<pre>
<code>var obj = new object();</code>
<code>lock(obj) { /* locked! */ }</code>
</pre>
</section>
</div></div>
<div class="slide" id="sync-thread"><div>
<section>
<header>
<h2>Thread synchronization</h2>
</header>
<ul>
<li>Every WinForms control has an <code>Invoke()</code> method</li>
<li>In WPF we can use the (generic) <code>Dispatcher</code> property</li>
<li>However, the most generic way of doing thread-safe UI calls is over the <code>SynchronizationContext</code> class</li>
<li>The static property <code>Current</code> carries the sync. context</li>
<li>This property is set by e.g. a WinForms <code>Form</code> instance</li>
</ul>
</section>
</div></div>
<div class="slide" id="example-thread-synchronization"><div>
<section>
<header>
<h2 class="example">→ Example - Thread synchronization</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day4/ThreadSync.cs" title="Download source" class="example-download">ThreadSync.cs</a>
</section>
</div></div>
<div class="slide" id="tasks"><div>
<section>
<header>
<h2>Tasks</h2>
</header>
<ul>
<li>Much more modern and powerful: Tasks!</li>
<li>Concept: Resources (threads, ...) are handled by the library</li>
<li>We have a very optimized (and managed) thread pool</li>
<li>Tasks can be connected, scheduled and synchronized</li>
<li>Important: Exceptions do not bubble up unless requested!</li>
<li>Nowadays everything is centered around the <code>Task</code> class</li>
</ul>
</section>
</div></div>
<div class="slide" id="task-parallel-library"><div>
<section>
<header>
<h2>The TPL</h2>
</header>
<ul>
<li>A set of useful classes and methods for tasks</li>
<li>Very elegant: using <code>Parallel.For()</code> for huge loops</li>
<li>Be careful with: race conditions & overhead due to creation</li>
<li>The TPL also introduced PLINQ (parallel LINQ)</li>
<li>Use parallel queries by calling the <code>AsParallel()</code> extension</li>
<li>Be aware of race conditions and shared resources</li>
<li>Another great feature of the TPL: new (concurrent) types</li>
</ul>
</section>
</div></div>
<div class="slide" id="tpl-structure"><div>
<section>
<header>
<h2>Structure of the TPL</h2>
</header>
<img src="pictures/tpl.png" class="middle r" />
</section>
</div></div>
<div class="slide" id="threading-considerations"><div>
<section>
<header>
<h2>Considerations for multi-threading</h2>
</header>
<ul>
<li>The workload has to be big enough</li>
<li>At least as much instructions as creating and ending the thread</li>
<li>Just running a problem on more cores is not equal more speed</li>
<li>Always think about IO-bound vs CPU-bound</li>
<li>IO-bound? 1 thread that is not the GUI thread is enough</li>
<li>CPU-bound? Up to <i>N</i> threads (for <i>N</i> cores) additional threads</li>
<li>Reduce communication to a minimum</li>
</ul>
</section>
</div></div>
<div class="slide" id="example-tpl"><div>
<section>
<header>
<h2 class="example">→ Example - Task Parallel Library</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day4/Tasks.cs" title="Download source" class="example-download">Tasks.cs</a>
</section>
</div></div>
<div class="slide" id="async-await"><div>
<section>
<header>
<h2>Await my async!</h2>
</header>
<ul>
<li>C# 5 introduces two new keywords: <code>await</code> and <code>async</code></li>
<li>By using <code>async</code> we mark functions as being asynchronous</li>
<li>This enables us to use the <code>await</code> keyword</li>
<li>This DOES not spawn a new thread</li>
<li>The default return value is <code>Task</code></li>
<li>The purpose is to write sequential code, which runs concurrently</li>
</ul>
</section>
</div></div>
<div class="slide" id="async-explained"><div>
<section>
<header>
<h2>Await my async!</h2>
</header>
<div class="left">
<img src="pictures/awaitasync.png" />
</div>
<div class="right">
<ul>
<li>The method is entered (doing UI)</li>
<li>Calling long-lasting function</li>
<li>The UI is meanwhile responsive</li>
<li>Once the task is finished ...</li>
<li>... the method is resuming</li>
<li>Doing some UI stuff again</li>
</ul>
</div>
</section>
</div></div>
<div class="slide" id="await-explained"><div>
<section>
<header>
<h2>Do's and don'ts</h2>
</header>
<ul>
<li><code>await</code> transforms a <code>Task<T></code> in <code>T</code></li>
<li>i.e. the variable <code>a</code> in <code>var a = await MyFoo();</code> of <code>async Task<int>Foo()</code> will be an integer</li>
<li>Really important here: We can use <code>try-catch</code></li>
<li>We could also use async over sync with <code>Task.Run()</code></li>
<li>Of course the opposite is also possible: sync over async</li>
<li>Here we just have to omit the <code>await</code></li>
</ul>
</section>
</div></div>
<div class="slide" id="async-gotchas"><div>
<section>
<header>
<h2>Common mistakes</h2>
</header>
<ul>
<li><code>async void</code> should only be used with event handlers</li>
<li>Using sync over async with an async function that switches to the UI</li>
<li>This will result in a deadlock, i.e. the UI is dead</li>
<li>Spawning too many (parallel) tasks</li>
<li>Using a lambda that returns <code>void</code> instead of <code>Task</code></li>
</ul>
</section>
</div></div>
<div class="slide" id="example-await-async"><div>
<section>
<header>
<h2 class="example">→ Example - Await and async</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day4/AwaitAsync.cs" title="Download source" class="example-download">AwaitAsync.cs</a>
</section>
</div></div>
<div class="slide" id="presentations"><div>
<section>
<header>
<h2>All available presentations</h2>
</header>
<div class="left">
<b>Week 1</b>
<ul>
<li><a href="day1.html">Presentation of Monday</a></li>
<li><a href="day2.html">Presentation of Tuesday</a></li>
<li><a href="day3.html">Presentation of Wednesday</a></li>
<li><a href="day4.html">Presentation of Thursday</a></li>
<li><a href="day5.html">Presentation of Friday</a></li>
</ul>
</div>
<div class="right">
<b>Week 2</b>
<ul>
<li><a href="day6.html">Presentation of Monday</a></li>
<li><a href="day7.html">Presentation of Tuesday</a></li>
<li><a href="day8.html">Presentation of Wednesday</a></li>
<li><a href="day9.html">Presentation of Thursday</a></li>
</ul>
</div>
</section>
</div></div>
<div class="progress"><div></div></div>
<script src="scripts/script.js"></script>
<!-- Copyright © 2013 Florian Rappl, www.florian-rappl.de -->
</body>
</html>