Multithreading: Multiple Mutex, Deadlock and Avoidance Technique!

Karan IITgn1,551 words

Full Transcript

So T1 is waiting for resource which is owned by T2 and T2 is waiting for some resource which is owned by T1 and neither of the threads are able to release the resource. So neither of the threads are able to execute the task. So this is how deadlock can happen in our system when Hi all this is me Karan. So in this particular lecture we are going to talk about mutx and deadlock. How the wrong uses of mutxes can create deadlock kind of situation in our system. So we will try to understand this and at the end we'll also try to discuss how we can avoid this kind of situation so that our system remains deadlog free. So let's start the discussion. So let's say in our system if only single mutx is there for example let's say there is this function function one and inside this function I am executing critical section code and here I'm using mutx mtx.lo and mtx dotunlock. So what will happen is thread T1 will try to execute this code and at the same time T2 can also try to execute this particular function. Let's say T1 gets the chance to execute function one and it locks this particular mutx. So ownership of this particular MTX is with T1. Now let's say thread T2 comes and it tries to execute function one. It checks whether it can lock the mutx or not. So system will say oh you cannot log the mutx because it is right now owned by this particular thread t1. So T2 will keep waiting at this point and when T1 will release this particular mutx then T2 will be able to execute this particular task. So whenever we are using a single mutx in our system there will be no problem no deadlock kind of situation will occur because at a time only one thread will lock the mutx and when it will complete its execution it will release the mutx so that other threads can use that particular mutx but whenever we are using multiple mutxes in our system our system might reach into the deadlock state. So in what kind of cases that happens let's try to understand that. So for better understanding I have taken two functions function one and function two. Inside function one I'm locking mutx one and then this particular function is doing some particular task. So it will take some time let's say 10 milliseconds and then I'm trying to lock mutx 2 and then again I'm doing some task. Let's say again this task takes 10 milliseconds and then unreleasing both the mutxes mtx1 and mtx2 using that unlock call. At the same time this function two locks the mutx 2 and then perform some task. Let's say this task takes again 10 millconds and then I lock mutx one and then do some task. Let's say again this task also takes 10 millconds and then I release both the mute axis. So let's say thread t1 try to access function one and t2 try to access function two. So what will happen is let's say CPU gets assigned to T1. So what T1 will do? T1 will lock the mutx1. So mutx1 will be logged by thread t_1 that is mtx1 will be owned by thread t1. Now this particular thread t1 do some particular task and this task takes around 10 millconds and in between context switch happens and and t2 gets the chance to execute function two. So now what happens is using this particular call t2 locks the mutx 2. So mutx 2 is owned by this particular thread t2. So t2 has locked this particular mutx. Now after some time this task is complete and t_1 tries to lock mutx 2. But the thing is mutx 2 is already owned by thread t2. So T1 will keep on waiting at this point for thread T2 to release this particular mutx 2 and in between context switch might happen and then T2 will try to lock this particular mutx MTX1 because it has completed uh this particular task do something and it will try to lock mutx1 but mutx1 is owned by this particular thread t1. So T2 will keep waiting at this point for T1 to release this particular mutx 1. So now what is happening is T1 is waiting for T2 to release this particular mutx and at the same time T2 is waiting for T1 to release this particular mutx. So both the threads are now dependent on each other that oh please release this particular mutx then I'll proceed ahead and t1 is saying oh please release this particular mutx then I'll be able to proceed ahead. So that way both the threads are stuck at this point and neither of the threads will be able to execute the task because neither T1 will be able to release mutx1 nor t2 will be able to release mutx2 because this particular mutx 2 will be released only when it receives the ownership of mtx1 and it completes the task and at the end it will try to unlock the mutx one sorry mutx So this is how deadlock happens in the system. So T1 is waiting for resource which is owned by T2 and T2 is waiting for some resource which is owned by T1 and neither of the threads are able to release uh the resource. So neither of the threads are able to execute the task. So this is how deadlock can happen in our system when we are not using multiple mutxes very carefully inside our system. So how we can avoid this kind of situation? Let's try to understand that. So instead of locking the mutx one and two and then two and one we should lock the mutxes in same order. That is inside this particular function one we should lock mutx one and then mutx 2. But in this particular function we should follow the same order that is first we should lock mutx one and then we should lock mutx 2. So whenever we are trying to use multiple mutxes in our system we need to make sure that we are locking them in the same order inside all the functions. For example, inside function one, I am first locking mutx one and then locking mutx 2. Similarly, inside function two, I'm locking mutx one and then mutx 2. So in this kind of situation, deadlock kind of situation won't happen. So now we'll try to dry run this particular code and we'll try to understand why deadlock kind of situation will not happen now. So let's say again I take same example. So let's say T1 is executing function one and again T2 is executing function two. So let's say CPU gets assigned to T1 and T1 locks this particular mutx 1 MTX 1. So this particular mutx 1 is owned by this particular thread. Now let's say CPU gets assigned to T2 and T2 tries to execute this particular function function 2 and it checks whether MTX1 is available for locking or not. But because mtx1 is used by this particular thread t_1. So t2 will keep waiting at this point and will not be able to perform the task. After certain time like after 10 milliseconds t1 will be able to complete this particular task and will try to lock this particular mtx2 because mtx2 is still free. it is not locked by any of the threads. So T1 will be able to lock this particular thread. So now MTX2 is also owned by this particular thread T1. And keep in mind T2 is still waiting at this point for MTX1 to be available which will be released by thread T1. Now after 10 milliseconds when T1 will be able to complete its task it will release mutx 1 and mutx2. Now when this particular mutx 1 will be released by thread t1 so t2 will be able to complete this task that is it will be able to lock this particular mutx because now this m mutx is available for locking. So now MTX1 will be logged by this particular thread T2 and T2 will perform some task and then at a later point in time MTX2 will also be available because it has already been logged unlocked by thread T1. So MTX2 will also be available for locking. So T2 will be able to lock this and it will be able to perform the task and at the end it will release both the mutexes. So MTX2 and MTX1. So MTX1 and MTX2 will be available for locking. So if we are using multiple mutexes inside our system and if we are locking all the mutxes in the same order chances are very high that there will be no deadlock kind of situation in our system or you can say almost 99.99% of the time there will be no deadlock or you can say no real deadlock in your system. I hope this particular lecture was helpful for you. So let's meet in the next lecture where we will try to implement this entire idea using C++. Thank you.

Need a transcript for another video?

Get free YouTube transcripts with timestamps, translation, and download options.

Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact

Multithreading: Multiple Mutex, Deadlock and Avoidance Te...