SLIDE 1 +
Section 3
Threading and Locking
SLIDE 2
+Definitions
What is a thread?
SLIDE 3
+Definitions
What is a thread? A single flow of control with a process
SLIDE 4
+Definitions
What is a thread? A single flow of control with a process Why use threads?
SLIDE 5
+Definitions
What is a thread? A single flow of control with a process Why use threads? Exploit latency, concurrency
Event-driven software
SLIDE 6
+Dangers
What could go wrong?
SLIDE 7
+Dangers
What could go wrong? Race Conditions
SLIDE 8
+Dangers
What could go wrong? Race Conditions How do we fix this?
SLIDE 9
+Dangers
What could go wrong? Race Conditions How do we fix this? Locking
SLIDE 10
+Definitions
What is a lock?
SLIDE 11
+Definitions
What is a lock?
Serializes access to some critical region of code or data Used to enforce mutual exclusion concurrency control
SLIDE 12
+Definitions
What is a lock?
Serializes access to some critical region of code or data Used to enforce mutual exclusion concurrency control Locks need help from hardware
SLIDE 13
+Definitions
What is a lock?
Serializes access to some critical region of code or data Used to enforce mutual exclusion concurrency control Locks need help from hardware Different kinds of locks
SLIDE 14
+Dangers
What could go wrong?
SLIDE 15
+Dangers
What could go wrong? Impacts Performance
SLIDE 16
+Dangers
What could go wrong? Impacts Performance
Hard to debug
SLIDE 17
+Dangers
What could go wrong? Impacts Performance
Hard to debug Deadlocks
SLIDE 18
+Dangers
What could go wrong? Impacts Performance
Hard to debug Deadlocks
How do we fix this?
SLIDE 19
+Dangers
What could go wrong? Impacts Performance
Hard to debug Deadlocks
How do we fix this? Punt (if collisions aren’t our problem)
SLIDE 20
+Dangers
What could go wrong? Impacts Performance
Hard to debug Deadlocks
How do we fix this? Punt (if collisions aren’t our problem)
Resource hierarchy/Conductor/Chandy-Misra
SLIDE 21
+Using Threads
Creation Termination Detachment and Joining Self and Equal
SLIDE 22
+Using Threads
Thread (class) Runnable (interface) ForkJoin ThreadPools/Executor Services
SLIDE 23
+Project 0 Review
SLIDE 24
+Project 1 Suggestions
John’s Suggestion
One thread for listening (waiting for server probes) One thread for keyboard input that handles server communication initiated by keyboard input One thread for periodic re-registration
SLIDE 25 +Project 1 Reminders
John’s Suggestion
One thread for listening (waiting for server probes) One thread for keyboard input that handles server communication initiated by keyboard input One thread for periodic re-registration
Remember Terminate cleanly
You need to re-register Modularity
SLIDE 26 +Questions?
Java:
http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html http://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/geninfo/diagnos/ thread_basics.html
C/Unix: http://www.mit.edu/people/proven/IAP_2000/index.html http://www.yolinux.com/TUTORIALS/ LinuxTutorialPosixThreads.html