Everything you should know about deadlock in three minutes or less Chris Kanich https://www.youtube.com/watch?v=oEbXlSH8hyE Transkript (automatisch erstellt) 0:00 this video is about deadlock deadlock is when a program can't make any forward progress due to broken locking this is about the easiest code 0:07 you can write that causes a deadlock in this code we're going to run two threads a jack thread and a jail thread 0:13 the jack thread locks x and then it lacks y and then it prints something it unlocks and it starts that process over again 0:19 jill locks y then locks x prints something and then releases both locks and repeats that if we run this program we'll see 0:26 very quickly that it ends up in deadlock ta-da immediately goes into deadlock didn't get to print a single time let's try it 0:32 one more time now here we see that it printed jack a whole bunch of times but right now there's nothing else happening on the screen we can take a 0:38 closer look at this by running it in the debugger here we see that we get started and we can continue from the pause on entry it runs 0:45 and then at this point in time i can manipulate this code nothing is happening now that we've paused these threads 0:50 let's take a look at where they're stuck the jack thread is attempting to gain access to the y mutex because it's on this line 0:57 that means it already got access to the x mutex the jail thread has access to the y mutex and is waiting to gain access to the x mutex 1:04 neither of these threads will ever finish all three of these threads are blocked waiting for something else to happen which will never happen 1:11 one helpful way to think about deadlock is that it's a situation that requires four conditions to actually happen condition one is that 1:18 you're using mutual exclusion locks such that only one thread can hold a resource at one time condition two is that each of these 1:25 threads gains access to their resources one at a time holds on to them and waits until they gain access to the next one condition three is that a thread cannot 1:32 be preempted that is it can't have its access to a lock revoked by another thread besides itself without 1:39 voluntarily deciding to give up that lock condition 4 is probably the most important in condition 4 what you need is a circular weight what 1:47 this means is that jack is waiting for jill to give up a resource and at the same time jill is waiting for 1:54 jack to give up a resource in this example there are two threads waiting on each other but any cycle between multiple threads would still 2:01 cause deadlock we can also see an example of deadlock in our train simulator as we see here there are two areas 2:07 protected by the train signals that act as mutual exclusion locks if i start both of these trains they will start moving but then eventually 2:14 they're going to end up in a deadlock situation as we see here one train is waiting for the other to leave that lock 2:20 but because it's so long it will hold on to that lock for quite some time now we see the deadlock one train is holding the lock 2:28 that the other one needs while that one is holding the lock that the first one needs this is a circular weight that can't be revoked 2:36 besides by deleting the train one of them is holding the lock and they're waiting until they get access to the next lock and is using 2:42 multiple locks this satisfies all four of our deadlock conditions this has been cs361 have a good one