Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
-
Upload
cori-jackson -
Category
Documents
-
view
214 -
download
0
Transcript of Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
![Page 1: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/1.jpg)
Concurrency
Recitation – 2/24Nisarg Raval
Slides by Prof. Landon Cox
![Page 2: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/2.jpg)
Threads
Light weight Easy and Fast Each thread has its own
SP, PC, Registers But they share
Heap, Code, Library Slow devices, interactive tasks, prioritization
Server addressing multiple clients
![Page 3: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/3.jpg)
Example
Two threads accessing account balance
Thread 1 deposit money
Thread 2 withdraw money
Initial balance is 100 What is the final balance?
![Page 4: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/4.jpg)
Debugging is Hard
Worst case reasoning Think of all ways threads can be
scheduled
Difficult to test all possible interleavings
Bugs may appear only sometimes Rerunning the program may make them
disappear!
![Page 5: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/5.jpg)
Constraining concurrency
Synchronization Controlling thread interleavings
Some events are independent No shared state Relative order of these events don’t
matter Other events are dependent
Output of one can be input to another Their order can affect program results
![Page 6: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/6.jpg)
Goals of synchronization
1. All interleavings must give correct result
Correct concurrent program Works no matter how fast threads run
Important for lab3!
2. Constrain program as little as possible
Why? Constraints slow program down Constraints create complexity
![Page 7: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/7.jpg)
“Too much milk” principals
![Page 8: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/8.jpg)
“Too much milk” rules
The fridge must be stocked with milk Milk expires quickly, so never > 1 milk
Landon and Melissa Can come home at any time If either sees an empty fridge, must buy milk Code (no synchronization)
if (noMilk){ buy milk;}
![Page 9: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/9.jpg)
Time
3:00 Look in fridge (no milk)
3:05 Go to grocery store3:10 Look in fridge (no milk)
3:15 Buy milk3:20 Go to grocery store3:25 Arrive home, stock fridge
3:30 Buy milk3:35 Arrive home, stock fridge
Too much milk!
Unsynchronized code will break
![Page 10: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/10.jpg)
What broke?
Code worked sometimes, but not always Code contained a race condition Processor speed caused incorrect result
First type of synchronization Mutual exclusion Critical sections
![Page 11: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/11.jpg)
Synchronization concepts
Mutual exclusion Ensure 1 thread doing something at a time E.g. 1 person shops at a time Code blocks are atomic w/re to each other Threads can’t run code blocks at same time
![Page 12: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/12.jpg)
Synchronization concepts
Critical section Code block that must run atomically
“with respect to some other pieces of code”
If A and B are critical w/re to each other Threads mustn’t interleave code from A and B A and B mutually exclude each other
Conflicting code is often same block But executed by different threads Reads/writes shared data (e.g. screen, fridge)
![Page 13: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/13.jpg)
Back to “Too much milk”
What is the critical section?
Landon and Melissa’s critical sections Must be atomic w/re to each other
if (noMilk){ buy milk;}
![Page 14: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/14.jpg)
“Too much milk” solution 1
Assume only atomic load/store Build larger atomic section from load/store
Idea:1.Leave notes to say you’re taking care of it
2.Don’t check milk if there is a note
![Page 15: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/15.jpg)
Solution 1 code
Atomic operations Atomic load: check note Atomic store: leave note
if (noMilk) { if (noNote){ leave note; buy milk; remove note; }}
![Page 16: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/16.jpg)
Does it work?
if (noMilk) { if (noNote){ leave note; buy milk; remove note; }}
if (noMilk) { if (noNote){ leave note; buy milk; remove note; }}
1 2
3 4
Is this better than no synchronization at all?What if “if” sections are switched?
![Page 17: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/17.jpg)
What broke?
Melissa’s events can happen After Landon checks for a note Before Landon leaves a note
if (noMilk) { if (noNote){ leave note; buy milk; remove note; }}
![Page 18: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/18.jpg)
Next solution
Idea: Change the order of “leave note”,
“check note” Requires labeled notes (else you’ll see
your note)
![Page 19: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/19.jpg)
Does it work?
leave noteLandonif (no noteMelissa){ if (noMilk){ buy milk; }}remove noteLandon
leave noteMelissaif (no noteLandon){ if (noMilk){ buy milk; }}remove noteMelissa
Nope. (Illustration of “starvation.”)
![Page 20: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/20.jpg)
What about now?
while (noMilk){ leave noteLandon if(no noteMelissa){ if(noMilk){ buy milk; } } remove noteLandon}
while (noMilk){ leave noteMelissa if(no noteLandon){ if(noMilk){ buy milk; } } remove noteMelissa}
Nope. (Same starvation problem as before)
![Page 21: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/21.jpg)
Next solution
We’re getting closer Problem
Who buys milk if both leave notes? Solution
Let Landon hang around to make sure job is done
![Page 22: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/22.jpg)
Too much milk solution
leave noteLandonwhile (noteMelissa){ do nothing}if (noMilk){ buy milk;}remove noteLandon
leave noteMelissaif (no noteLandon){ if (noMilk){ buy milk; }}remove noteMelissa
![Page 23: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/23.jpg)
Downside of solution
Complexity Hard to convince yourself it works
Asymmetric Landon and Melissa run different code
Not clear if this scales to > 2 people Landon consumes CPU while waiting
Busy-waiting
Note: only needed atomic load/store
![Page 24: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/24.jpg)
Raising the level of abstraction
Locks Also called mutexes Provide mutual exclusion Prevent threads from entering a critical
section Lock operations
Lock (aka Lock::acquire) Unlock (aka Lock::release)
![Page 25: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/25.jpg)
Lock operations
Lock: wait until lock is free, then acquire it
This is a busy-waiting implementation We’ll improve on this in a few lectures
Unlock: atomic release lock
do { if (lock is free) { acquire lock break }} while (1)
Must be atomic with respect to other threadscalling this code
![Page 26: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/26.jpg)
Too much milk, solution 2
Why doesn’t the note work as a lock?
if (noMilk) { if (noNote){ leave note; buy milk; remove note; }}
Block is not atomic.Must atomically• check if lock is free• grab it
![Page 27: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/27.jpg)
Elements of locking
1. The lock is initially free2. Threads acquire lock before an action3. Threads release lock when action
completes4. Lock() must wait if someone else has
lock Key idea
All synchronization involves waiting Threads are either running or
blocked
![Page 28: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/28.jpg)
Too much milk with locks?
Problem? Waiting for lock while other buys milk
lock ()if (noMilk) { buy milk}unlock ()
lock ()if (noMilk) { buy milk}unlock ()
![Page 29: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/29.jpg)
Too much milk “w/o waiting”?
lock ()if (noNote && noMilk){ leave note “at store” unlock () buy milk lock () remove note unlock ()} else { unlock ()}
Only hold lock while handling shared resource.
lock ()if (noNote && noMilk){ leave note “at store” unlock () buy milk lock () remove note unlock ()} else { unlock ()}
Not holdinglock
![Page 30: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/30.jpg)
What about this?
lock ()if (noMilk && noNote){ leave note “at store” unlock () buy milk stock fridge remove note} else { unlock ()}
lock ()if (noMilk && noNote){ leave note “at store” unlock () buy milk stock fridge remove note} else { unlock ()}
lock ()if (noMilk
12
3 4
![Page 31: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/31.jpg)
What about Java? Too much milk
Every object is a lock Use synchronized key word
Lock : “{“, unlock: “}”
synchronized (obj){ if (noMilk) { buy milk }}
synchronized (obj){ if (noMilk) { buy milk }}
![Page 32: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/32.jpg)
Concurrency in Java
Implement a Runnable Interface public class MyThread implements
Runnable{ public void run(){} }
Subclass Thread public class MyThread extends Thread{ public void run(){} }
![Page 33: Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649de55503460f94adc9b7/html5/thumbnails/33.jpg)
Example