-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
440 lines (432 loc) · 11.9 KB
/
index.html
File metadata and controls
440 lines (432 loc) · 11.9 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Concurrency & Ruby Implementations</title>
<meta content="" name="description" />
<meta content="" name="author" />
<link href="css/reveal.css" rel="stylesheet" />
<link href="css/simple.css" rel="stylesheet" />
<link href="css/googlecode.css" rel="stylesheet" />
</head>
<body>
<div class="reveal">
<div class="slides"><section class="center" data-background="#000">
<h2 class="white">
Concurrency & Ruby Implementations
</h2>
</section>
<section class="center">
<p>
This Thursday we have a special guest
</p>
</section>
<section class="center">
<p>
Don't miss Testing Ruby by Radoslav Stankov
</p>
</section>
<section class="center" data-background="#000">
<h2 class="white">
Questions
</h2>
</section>
<section class="center">
<p>
What is a scope gate?
</p>
</section>
<section class="center">
<p>
Which are the keywords that introduce scope gates?
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">def feed(*meals)
super()
end </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">def feed(*meals)
super
end </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">o = Object.new
def o.to_a
[o]
end </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">o = Object.new
class << o
def to_a
[o]
end
end </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">result == class Foo
class << self
self
end
end
Foo.singleton_class == result </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">o = Object.new
def o.to_str
bar
end
"foo" + bar </code></pre>
</section>
<section class="center">
<p>
What is this called?
</p>
</section>
<section class="center">
<p>
What do we use it for?
</p>
</section>
<section class="center">
<p>
What does instance_eval does?
</p>
</section>
<section class="center">
<p>
What does class_eval does?
</p>
</section>
<section class="center">
<p>
What's the difference?
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">Object.superclass </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">BasicObject.superclass </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">Class.new.superclass </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">Class.new(Array).superclass </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">Module.new.superclass </code></pre>
</section>
<section class="center">
<img class="inline" src="img/troll.png" />
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">A = Class.new
B = Class.new(A)
A.singleton_class.superclass == # ? </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">A = Class.new
B = Class.new(B)
A.singleton_class.superclass == B.singleton_class </code></pre>
</section>
<section class="center">
<img class="inline" src="img/troll.png" />
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">A = Class.new
B = Class.new(B)
B.singleton_class.superclass == A.singleton_class </code></pre>
</section>
<section class="center" data-background="#000">
<p class="white">
The _____ class of the _________ class is the _________ class of the _____ class
</p>
</section>
<section data-background="http://www.boomerang-academy.fr/operations/gif-adventure-time/gif1.gif"></section>
<section class="center" data-background="#F00">
<h2 class="white">
Global Interpreter Lock
</h2>
</section>
<section class="center">
<p>
Global Interpreter Lock (GIL) is a mechanism used in computer language interpreters to synchronize the execution of threads so that only one thread can execute at a time
</p>
</section>
<section class="center">
<p>
An interpreter which uses GIL will always allow exactly one thread to execute at a time, even if run on a multi-core processor
</p>
</section>
<section class="center">
<p>
GIL is a mutual exclusion lock held by a programming language interpreter thread to avoid sharing code that is not thread-safe with other threads
</p>
</section>
<section class="center">
<p>
Effectively limits the amount of parallelism reachable through concurrency of a single interpreter process with multiple threads
</p>
</section>
<section class="center" data="#000">
<p>
CRuby uses a Global Interpreter Lock
</p>
</section>
<section class="center">
<p>
Affects CPU-bound programs and not IO-bound programs, which are the majority of Ruby programs
</p>
</section>
<section class="center" data-background="#0F0">
<h2 class="white">
Threads
</h2>
</section>
<section class="center">
<p>
Threads are the Ruby implementation for a concurrent programming model
</p>
</section>
<section class="center">
<p>
Programs that require multiple threads of execution are a perfect candidate for Ruby's Thread class
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">thr = Thread.new { puts "Whats the big deal" } </code></pre>
</section>
<section class="center">
<p>
We can pause the execution of the main thread and allow our new thread to finish with Thread#join
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">thr.join #=> "Whats the big deal" </code></pre>
</section>
<section class="center">
<p>
If we don't call thr.join before the main thread terminates, then all other threads including thr will be killed
</p>
</section>
<section class="center">
<p>
Alternatively, you can use an array for handling multiple threads at once
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">threads = []
threads << Thread.new { puts "Whats the big deal" }
threads << Thread.new { 3.times { puts "Threads are fun!" } } </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">threads.each { |thr| thr.join } </code></pre>
</section>
<section class="center" data-background="#00F">
<h2 class="white">
Initialization
</h2>
</section>
<section class="center">
<p>
In order to create new threads, Ruby provides ::new, ::start, and ::fork
</p>
</section>
<section class="center">
<p>
A block must be provided with each of these methods, otherwise a ThreadError will be raised
</p>
</section>
<section class="center">
<p>
When subclassing the Thread class, the initialize method of your subclass will be ignored by ::start and ::fork
</p>
</section>
<section class="center">
<p>
Otherwise, be sure to call super in your initialize method
</p>
</section>
<section class="center" data-background="#00F">
<h2 class="white">
Termination
</h2>
</section>
<section class="center">
<p>
For terminating threads, Ruby provides the class method ::kill
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">thr = Thread.new { ... }
Thread.kill(thr) # sends exit() to thr </code></pre>
</section>
<section class="center">
<p>
Alternatively, you can use the instance method exit, or any of its aliases kill or terminate
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">thr = Thread.new { ... }
thr.kill </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">thr = Thread.new { ... }
thr.exit </code></pre>
</section>
<section class="center" data-background="#00F">
<h2 class="white">
Status
</h2>
</section>
<section class="center">
<p>
Ruby provides a few instance methods for querying the state of a given thread
</p>
</section>
<section class="center">
<p>
To get a string with the current thread's state use status
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">thr = Thread.new { sleep }
thr.status # => "sleep"
thr.exit
thr.status # => false </code></pre>
</section>
<section class="center">
<p>
You can also use alive? to tell if the thread is running or sleeping, and stop? if the thread is dead or sleeping
</p>
</section>
<section class="center" data-background="#00F">
<h2 class="white">
Variables & Scope
</h2>
</section>
<section class="center">
<p>
Threads are created with blocks
</p>
</section>
<section class="center">
<p>
The same rules apply to other Ruby blocks for variable scope
</p>
</section>
<section class="center">
<p>
Any local variables created within this block are accessible to only this thread
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">Thread.new do
Thread.current.thread_variable_set(:foo, 1)
p Thread.current.thread_variable_get(:foo) # => 1
end </code></pre>
</section>
<section class="center">
<p>
Each fiber has its own bucket for #[] storage
</p>
</section>
<section class="center">
<p>
When you set a new fiber-local it is only accessible within this Fiber
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">Thread.new do
Thread.current[:foo] = "bar"
Fiber.new do
p Thread.current[:foo] # => nil
end.resume
end.join </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">Thread.new do
Thread.current.thread_variable_set(:foo, 1)
p Thread.current.thread_variable_get(:foo) # => 1
Fiber.new do
Thread.current.thread_variable_set(:foo, 2)
p Thread.current.thread_variable_get(:foo) # => 2
end.resume
p Thread.current.thread_variable_get(:foo) # => 2
end.join </code></pre>
</section>
<section class="center" data-background="#00F">
<h2 class="white">
Exceptions
</h2>
</section>
<section class="center">
<p>
Any thread can raise an exception using the #raise instance method, which operates similarly to Kernel#raise
</p>
</section>
<section class="center">
<p>
However, it's important to note that an exception that occurs in any thread except the main thread depends on abort_on_exception
</p>
</section>
<section class="center">
<p>
This option is false by default, meaning that any unhandled exception will cause the thread to terminate silently when waited on by either join or value
</p>
</section>
<section class="center">
<p>
You can change this default by either abort_on_exception= true or setting $DEBUG to true
</p>
</section>
<section class="center">
<p>
With the addition of the class method ::handle_interrupt, you can now handle exceptions asynchronously with threads.
</p>
</section>
<section class="center" data-background="#000">
<p class="white">
A programmer had a problem, so he decided to use threads
</p>
</section>
<section class="center" data-background="#000">
<p class="white">
Now 2 has He problems
</p>
</section>
<section data-background="http://media.giphy.com/media/Ed46JnKn4dwY0/giphy.gif"></section>
</div>
</div>
<script src="js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script type="text/javascript">
Reveal.initialize({
width: '100%',
height: '100%',
margin: 0.05,
controls: false,
progress: false,
history: true,
center: false,
rollingLinks: true,
transition: 'none',
backgroundTransition: 'none',
dependencies: [
{
src: 'js/highlight.js',
async: true,
callback: function() {
hljs.configure({ languages: ['ruby'] });
hljs.initHighlightingOnLoad();
}
}
]
});
</script>
</body>
</html>