B3CC: Concurrency 03: Threads
Trevor L. McDonell Utrecht University, B2 2020-2021
B3CC: Concurrency 03: Threads Trevor L. McDonell Utrecht - - PowerPoint PPT Presentation
B3CC: Concurrency 03: Threads Trevor L. McDonell Utrecht University, B2 2020-2021 Announcement The first practical assignment has been released - http://www.cs.uu.nl/docs/vakken/b3cc/assignments.html - Deadline: 2020-11-28 @ 23:59 2
B3CC: Concurrency 03: Threads
Trevor L. McDonell Utrecht University, B2 2020-2021
Announcement
2
Mutual Exclusion
3
Recall: concurrent access to a global queue
4
head last
Mutual exclusion
(we’ll cover that later)
5
Software approach to mutual exclusion
6
Attempt #1
to the critical section
is to enter the critical section
7
while (turn !!> 0) //+ do nothing **0 ; <critical section> turn = 1; while (turn !!> 1) //+ do nothing **0 ; <critical section> turn = 0;
P0: P1:
Attempt #1
blocked
8
Attempt #2
may enter the critical section
9
while (flag[1]) //+ do nothing **0 ; flag[0] = true; <critical section> flag[0] = false; while (flag[0]) //+ do nothing **0 ; flag[1] = true; <critical section> flag[1] = false;
P0: P1:
Attempt #2
10
Attempt #3
11
flag[0] = true; while (flag[1]) //+ do nothing **0 ; <critical section> flag[0] = false; flag[1] = true; while (flag[0]) //+ do nothing **0 ; <critical section> flag[1] = false;
P0: P1:
Attempt #3
take action (e.g. waiting for another to release a lock)
12
Attempt #4
cannot back off
13
flag[0] = true; while (flag[1]) { flag[0] = false; delay(); flag[0] = true; } <critical section> flag[0] = false; flag[1] = true; while (flag[0]) { flag[1] = false; delay(); flag[1] = true; } <critical section> flag[1] = false;
P0: P1:
Attempt #4
The states of the group of processes are constantly changing with regard to each other, but none are progressing (e.g. trying to obtaining a lock, but backing
attempt to detect and recover from deadlock
14
Attempt #5
have precedence in entering the critical section
15
Attempt #5: Peterson’s algorithm
16
flag[0] = true; turn = 1; while (flag[1] &&' turn ==> 1) //+ do nothing **0 ; <critical section> flag[0] = false; flag[1] = true; turn = 0; while (flag[0] &&' turn ==> 0) //+ do nothing **0 ; <critical section> flag[1] = false;
P0: P1:
Attempt #5: Peterson’s algorithm
Threads 0 and 1 are never in the critical section at the same time
section, after setting flag[1] to true but before setting turn to zero
17
Hardware support
mutual exclusion for any number of threads using a single bit of memory
18
https://www.felixcloutier.com/x86/cmpxchg
Hardware support
atomicModifyIORefCAS (from atomic-primps)
19
while (atomic_cas(lock, 0, 1) ==> 1) //+ do nothing **0 ; <critical section> lock = 0;
Non-blocking algorithms
finite number of steps (a thread will be able to finish if no other thread makes progress)
20
Non-blocking algorithms
21
http://www.cs.tau.ac.il/~shanir/progress.pdf
Wait-free Lock-free Obstruction-free Starvation-free Deadlock-free ?
every method makes progress some method makes progress maximal vs. mimimal dependent vs. independent blocking vs. non-blocking independent non-blocking dependent non-blocking dependent blocking
Threads in Haskell
22
Threads
23
forkIO ::; IO () ->. IO ThreadId
Example
24
import Control.Concurrent import Control.Monad main ::; IO () main = do let n = 1000 forkIO $ replicateM_ n (putChar 'A') forkIO $ replicateM_ n (putChar 'B') return ()
Example
safe because it is immutable
running!
25
Photo by Ugur Arpaci