Introduction to Concurrency CS 5334/4390 Lecture January 16, 2017 Shirley Moore svmoore@utep.edu
Motivation for Studying Concurrency Most of my subjects (interviewees) have found that the hardest bugs to track down are in concurrent code … And almost every one seems to think that ubiquitous multi-core CPUs are going to force some serious changes in the way software is written. P. Siebel, Coders at Work (2009) -- interviews with 15 top programmers of our times: Jamie Zawinski, Brad Fitzpatrick, Douglas Crockford, Brendan Eich, Joshua Bloch, Joe Armstrong, Simon Peyton Jones, Peter Norvig, Guy Steele, Dan Ingalls, L Peter Deutsch, Ken Thompson, Fran Allen, Bernie Cosell, Donald Knuth
Concurrent Programming is Error-prone Correctness of concurrent programs is hard to achieve Interactions between threads are hard to reason about Thread executions with non-deterministic thread scheduling Testing techniques for sequential programs do not work 4 processes, 55043 states 3 processes, 853 states 2 processes , 30 states Input space v.s. thread scheduling space which is not under-control Peterson mutual exclusion (From Dr. Moritz Hammer’s Visualisierung ) Slide courtesy of Moonzoo Kim at KAIST
Concurrency example Concurrent programs have very high complexity due to non-deterministic scheduling Ex. int x=0, y=0, z =0; void p() {x=y+1; y=z+1; z= x+1;} void q() {y=z+1; z=x+1; x=y+1;} 20 interleaving scenarios = (3+3)!/(3!x3!) 11 unique outcomes p() x=y+1 y=z+1 z=x+1 q() y=z+1 z=x+1 x=y+1 x,y,z 2,1,2 2,1,3 2,2,3 2,3,3 2,4,3 3,2,3 4,3,2 4,3,5 Trail1: 2,2,3 Trail2: 3,2,4 Trail3: 3,2,3 Trail4: 2,4,3 Trail5: 5,4,6 Trail6: 5,4,3 Trail7: 2,1,3 Trail8: 2,3,3 Trail9: 4,3,5 Trail10: 4,3,2 Trail11: 2,1,2 Slide courtesy of Moonzoo Kim at KAIST
Very difficult to find concurrency bugs !!!
Call Stack for a Sequential Program
Call Stacks for a Multithreaded Program
Classic Concurrency Bugs Nondeterminism (not a bug by itself, but can make bugs hard to find) Example: Threads1.java Question: What causes the nondeterministic behavior? Race condition Example: PrimeCount<n>.java Question: Why is the result from PrimeCount3 incorrect when multiple threads are used? Deadlock and starvation: Example: DiningPhilosophers.java Group activity: Design a solution that prevents deadlock Design a solution that prevents both deadlock and starvation
Feedback on today’s class (do this now!) Send email to svmoore@utep.edu with answers to these three questions: What was the most important thing you learned today? What did you learn today that most surprised you? (Ok to say nothing if nothing surprised you) What are you most unclear about from today’s class? (Ok to say nothing if everything was crystal clear)
Preparation for Next Class (do this now!) We’ll be using Stampede for this course, starting next class. Go to portal.xsede.org and create an XSEDE portal account if you don’t already have one. Send you XSEDE username to svmoore@utep.edu to be added to the education allocation for the course.