[Music] hello guys this is rotation watching CP peanut videos and C++ and today's topic is lock guard in C++ threading and this is the syntax for that so you'll be having namespace and then log guard and yes this is a class I mean a template class so you have to use the type so you will pass the type and this is your variable name and this is the mutex so you have to always pass the mutex when you create a log card object okay so this is object and this is mutex object so before going for this nodes let's look at this program first this program is creating two threads this one and this one and I am passing this thread number as T 0 and T 1 okay and this 10 is for this loop for and it will increment this particular buffer 10 times so this thread will do this increment for 10 time and another thread we also do the same increment for the 10 time so ultimately this buffer will become 20 so we'll print this buffer in the end we'll see that ok it is getting 20 because initially it is 0 so these two threads are trying to modify this buffer at the same time that's why we will use this mutex lock and mutex unlock we know this very well how it work okay if you don't know how mutex lock and unlock work you go ahead and watch my previous videos on mutex but still let me quickly tell you that how it will work so t1 and t2 any of these can start this thread at any order so let's suppose t1 started this and Tevan will able to lock this mutex first so t2 have to wait here until unless t1 unlocks this matrix so this is how it works now what if I tell you that you don't have to unlock it because you can see that when you are done I mean when you are done with this for loop and going out of this scope and you are about to terminate your thread before that only you are unlocking your mutex and the moment you started this thread you loved it so instead of this lock and unlock you can use this log guard which will do this if you will write log guard here let me show you that example now so this is the example with the mutex and if you will see this this is the example with non-mutex with lock guard and as I said this is the syntax for creating lock guard and instead of writing mutex m1 dot lock and m1 dot unlock what this will do you will just create this and the moment you create it it will try to log this m1 okay so it is a wrapper over this m1 so that's the first point it is very lightweight wrapper for owning a mutex on scoped basis what is this scope basis we'll see that but the main point is it is a wrapper for owning the mutex okay so both thread 1 and thread 2 will try to lock this mutex m1 with this help of log guard okay so you just understand that whenever you will create the object of the log guard you have to pass the mutex and it will try to log that mutex at that place only so that you don't have to write lock/unlock and all that ok so assume that it is logged now as I said let's suppose t1 logged it then t2 will have to wait here ok then t1 will go inside this for loop and will execute this buffer I mean increment this buffer for 10 time and it will print that for 10 time and once it will come out of this for loop and the moment it is going out I mean this thread t1 is completed in the destructor of this log guard it will unlock this mutex this is how it work so you create the object and pass the mutex it will lock at that point only and whenever the scope of this particular object is Oh it will try to unlock it so this is kind of an automatic behavior so let's read the point now it is very lightweight wrapper for owning the mutex see there is owning this m1 okay so it will own the mutex on scope basis see as I said when it will go out of this scope I mean when this function call is over by this thread 0 then in that case it will unlock it and the second point is it acquires mutex lock the moment you create the object of lock guard that's what I said when you create the object it will lock the mutex ok third point is it automatically removes the lock while goes out of scope exactly that's what we saw that and fourth is you cannot explicitly unlock the lock guard and this is very important point this point number four it says that I mean point number three it says that you cannot explicitly unlock the lock guard as you was doing in this example here you cannot do that because this is working on scope basis so it will unlock it automatically when the lock will go out of scope and it will go out of scope when this boundary will hit because this lock was created in this scope okay starting in ending curly braces so this is direct to this function now if you will say that let's suppose we have this task function task and this is starting curly braces and this is ending curly braces and there is this another start and early and curly braces here and if you write lock guard here then what then yes then it will come out of this scope then it will call the destructor of this and the last point is you cannot copy log guard exactly you cannot have I mean you cannot pass this ownership of the mutex you cannot move this so now you will ask that when should I use this lock because there is something called you Clark if you have already gone through that unique lock people are confused like when should they use this one and this one so actually you can use this when you want to give a explicit message to the viewer of your code that after owning this mutex I don't want to release this mutex until unless the scope is finished so it will tell you the user that okay you wanted the full scope I mean you wanted this mutex alive till the full scope wherever it was created okay I mean wherever this object was created a lock object and about this unique lock we will see in the next video so let's run this and sum this up so I have compiled it compiled successfully now I will run this ct0 started it and the one will start with 11 because buffer was incremented till 10 by thread 0 you can see this okay and if you don't know how all these work you please go ahead and watch my previous videos on threading and watch it from the beginning so that you don't have any gap and don't be confused that t1 is written first so t0 is started first know even t1 can start first let me try to give you this example I will run this again and again and see see see see this just before this see this time I was running so many time and this time see this t1 started first and then t2 started okay sorry t1 started first and these 0 started after that I should change this order okay but you understood right so t1 is created later but the one was created first and t0 was created later so I guess you would have enjoyed my video thanks for watching guys and don't forget to hit the like button if you liked the video don't forget to hit the like button it will help me a lot I will see in the next videos bye bye
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