Difference between trylock and lock in C
Вставка
- Опубліковано 4 лис 2024
- Source code can be found here:
code-vault.net...
===== Support us through our store =====
code-vault.net...
===== Check out our website =====
code-vault.net
===== Check out our Discord server =====
discord.code-v...
I have watched a few of your videos. I have to say... you explain things well, and you get to the nuts and bolts. Thank you and keep it up! =)
Code vault coming in clutch again, thank you!
Great explaination thank you
thanks for the explanation!
So the reason to use trylock is make the waiting threads do something if else if the locked area is used, instead of just wait
Exactly
If I don't care what is executed when I don't get the lock or if I don't want anything to execute when I don't get it, would it be okay to just call trylock and not checking for it's value?
I guess you could... but I am not sure about the use-case of that. Usually you try_lock because you want to do something based on if you got the lock or not. If you do the same for both cases... then why even call try_lock?
Thank you for this video, I am watching all your videos. May i know your name please..
Why is there no warning, for reaches end of non-void function (routine)?
If you compile with
gcc -Wall -o main main.c
that warning will appear. Best practice would be to return NULL; in the routine function
when mutex_lock was not successful, it can sleep(schedule for other thread). When mutex_trylock() fails, it does the same thing? What is the main difference when mutex_lock and mutex_trylock failed to get the lock?
If mutex_lock can't acquire the lock, it has to wait to until it can.
If mutex_trylock can't acquire t he lock, it simply returns a status code and continues execution.
@@CodeVault Thanks a lot for your reply. I maybe confused with spinlock() where it spins when it can't obtain the lock.(so it's used in interrupt context where it can't sleep), and as I know, mutex_lock when it fails it can sleep, so it is used in userspace/process context. However based on your comment, spinlock() and mutex_lock() are same(they wait(spin) until they can acquire lock). Am I understanding it correctly? Thanks!
Yeah, a spinlock will still wait... but it's just a continuously running loop that checks really really often. It wouldn't be a loop if it didn't wait :D
@@CodeVault Thanks, it would be great, if you go over pthread spinlock as well and compare the difference between mutex/spinlock. Thanks!
If trylock == 0 that means it was not locked, so it locks itself then enters the critical section?
Yes, you can read more about this function on the docs page: linux.die.net/man/3/pthread_mutex_trylock
THX
What does errno.h do?
It's just a header file that has all the error types defined and a few utilities to get the current error: man7.org/linux/man-pages/man3/errno.3.html
slow down man god damn
I think UA-cam has a slow down feature. If I go too fast, I'm sorry about that. Although I don't want videos to be unnecessarily long and waste time like many other tutorials on the platform.
@@CodeVault Don't worry about your speed I think its perfect. Easy to understand and concise. I watched in 2x speed and it's just right for me. If @Damage needs to go slower he can use the lower speeds or pause as you mentioned.
also agree, your speed is perfect! Thank you for your videos! @@CodeVault
for me he goes too slow and i have to put him on 1.5x or even 2x sometimes. Thats the good thing about watching a video instead of being in a lecture in person. You can change the speed at which the video plays so you're free to learn at the pace that fits your taste the best.