Difference between trylock and lock in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video i want to take a look at the pthread mutex try lock function and what it actually does and what is different between it and the lock function and the one that we used previously to do that i'm going to have to first create at least let's say four threads and i just want to see the behavior of peter mutex lock first so i'm just gonna create a very simple program that does exactly that and here inside the routine what i want to do is simply lock a single mutex by all the four threads that we have created and print a message and see what happens so we're gonna create here all right and then simply just call p thread mutex lock on our mutex and of course also call unlock after we are done and in between we're just gonna print a simple message that we got the lock and after we got the lock let's say it does something that is quite time consuming so i'm gonna just simply call a sleep here instead of actually expecting something that's time consuming suppose that's something there and if i try to run this think about what's gonna happen and what's gonna show on the terminal well see because we have four threads doesn't mean that we're not gonna get four got lock messages right you just get that we're gonna get them in succession of one one second one after the other because what's gonna happen well all four threads are gonna start executing right the same exact function but only one of them we're gonna are gonna be able to lock that mutex right the rest are gonna have to wait okay and then only after that the single thread that i got the lock unlock that mutex then another thread can continue and so on and so forth and that's what's basically happening here all right then what's so different about try lock well if i change this to a try lock instead of lock well this is a bit more tricky because with try lock you are trying to lock the mutex it's not guaranteed that you got it but if you did that's great but if you didn't you shouldn't be executing uh the code in the critical section therefore you should check the return value of trilog always so the return value of try lock is zero if you got the lock or is something else if you didn't it's actually e busy if if you didn't get the log so we can say here if trilog equals zero then we can execute this printf this slip and of course if we go to lock we should also unlock it so that unlock call still applies that's exactly the same okay and let's say on else we can say that well if it's not zero that means it's uh the constant ebz which i think it's actually in i don't know.h here if i type in ebz yeah it's 16 so that's what we got uh we get if we if our trilog function tries to lock the mutex but it's already locked okay and just print out another message saying that i didn't get lock okay that's it now if i try to launch this think about what's gonna happen in this case you might notice that you don't actually get four got lock messages right you only get one got lock message of the first thread that got the lock here but the rest tried to get the lock that failed and the execution continued on the else branch because the result wasn't zero so it just printed didn't get the lock and that's the difference between try lock and lock basically one of them is instant and the other one is gonna be waiting okay so lock is going to be always waiting until the mutex is unlocked but try lock is just going to say okay well uh are you locked if if if you're not locked then okay that's fine you can lock and execute the code but if you're locked then that's also fine i'm just going to return i'm not going to wait i'm going to return out of the function with a number that's different than 0 here and uh the programmer knows what to do from there right like i did here and that's really all there is to it it's very i think it's a very simple concept to grasp uh in a future video we're gonna take a look at uh certain examples that make use of this try lock function because it's it's very interesting and has some uh nice uses uh although the examples are quite quite a bit more complex than the usual it's uh it's probably fine all right thank you guys so much for watching if you do have any questions leave them down comments below or on our discord server of course the source code is going to be found on our website link down in the description below take care bye
Info
Channel: CodeVault
Views: 2,825
Rating: 5 out of 5
Keywords: codevault, c (programming language), trylock, lock, mutex, pthread, threads
Id: OIKr2ll2Nd8
Channel Id: undefined
Length: 6min 4sec (364 seconds)
Published: Fri Dec 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.