 Hi everyone. In this video, we will learn about multi-threading. We will write a C program which spawns multiple threads and which accesses a global counter in both the threads. So I have written this multi-threading.c program. Let's open it in visual code. So this is the multi-threading.c program. Let's go over the code. We have this global counter variable and in the main function we initialize this counter variable to 0. Then there is this increment by a 10,000 function which increments this counter variable by 10,000. So it runs a loop for 10,000 times and each times it adds one to the counter variable. So here we use pthread library which is the POSIX threads library which allows us to create multiple threads. We first declare two threads T1 and T2. So we use the pthread create function to create the threads. So first we create a T1 thread and we do not specify any attribute and we give it increment by 10,000 function so that this thread runs this increment by 10,000 function and because increment by 10,000 doesn't take any argument, so we specify null here. Then we create the T2 thread again using the same increment by 10,000 function and again we do not specify any arguments. Then we use this pthread join function so that the parent thread waits for thread T1 to complete and similarly it waits for the thread T2 to complete. So once both threads have finished execution then the parent thread pins the final counter value. So we expect that because we are incrementing this counter by 10,000 two times so the final counter value should be 20,000. So let's compile this program and run it. So I'll open that terminal and compile it using GCC because we are using the pthread library so we need to specify that while compiling. Okay so this has created this arrowed out file let's run this arrowed out. So you can see that the counter value is less than 20,000. Let's run it a couple of more times. So every time we get some different value and which is less than 20,000. So why is that so? This is because there is a race condition in the code and what do I mean by race condition? So there's this line in the code which mentions counter equal to counter plus one. So when it is translated to assembly then there are three instructions. It first loads this counter value in a register then it increments the value in the register by one and finally it again stores back the register value to the counter variable because there are two threads which are accessing this part of code concurrently. So it might happen that first thread execute this instruction it loads the counter value in the inner register it increments the register value by one but before it stores back the counter value back to the counter variable the another thread execute this instruction so it takes in the previous value it also increments it by one and finally both the threads store the same value to the counter valuable. In a sense instead of increasing the counter value by two their execution just increases the counter value by one. So how do we avoid this race condition? So you can get rid of this race condition using loax. We will look the critical section of the code which is this line so that only one thread can execute it at a time. So first we will declare a loach p thread mutex t and we'll call this loach m. So this mutex stands for mutual exclusion because we want mutual exclusion for this part of code. So we acquire loach before entering the critical section use p thread mutex loach and we give it a pointer to m. Then after executing the counter equal to counter plus one we unlock it so that another thread can access the loach and run this part of code. So we use p thread mutex unlock m. So now before entering the critical section each of the thread will have to acquire the loach and then they'll release it after exiting from the critical section. So only one thread will be able to execute this part of code. Now what will happen is first thread will acquire the loach then when it is executing counter equal to counter plus one even if the second thread reaches this particular part of code it will be unable to acquire the loach because first thread already has the loach and only when first thread releases the loach after this particular code will the second thread be able to acquire it and execute counter equal to counter plus one. So this is how we can get rid of the race condition. So let's compile and run it again. So I'll open the terminal and compile it again with p thread library. So now you see we are getting 20,000 every time we run the code. So it is very important to use this mutual exclusion loach whenever you are accessing a global variable in case of multi-setting program. So that's it for this video. Thanks and have a nice day.