Difference between trylock and lock in C

  Рет қаралды 27,212

CodeVault

CodeVault

Күн бұрын

Пікірлер: 26
@LightLord1870
@LightLord1870 3 жыл бұрын
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! =)
@tungbigbang98
@tungbigbang98 2 жыл бұрын
Code vault coming in clutch again, thank you!
@oussamasallak9490
@oussamasallak9490 2 жыл бұрын
Great explaination thank you
@andrelemospiucco5364
@andrelemospiucco5364 Ай бұрын
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
@CodeVault
@CodeVault Ай бұрын
Exactly
@jackh4158
@jackh4158 Жыл бұрын
thanks for the explanation!
@stefanknezevic6362
@stefanknezevic6362 2 жыл бұрын
Why is there no warning, for reaches end of non-void function (routine)?
@CodeVault
@CodeVault 2 жыл бұрын
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
@narasarajv5278
@narasarajv5278 3 жыл бұрын
Thank you for this video, I am watching all your videos. May i know your name please..
@caiomazzaferroadami
@caiomazzaferroadami 6 ай бұрын
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?
@CodeVault
@CodeVault 6 ай бұрын
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?
@Codality
@Codality 2 жыл бұрын
THX
@js4773
@js4773 3 жыл бұрын
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?
@CodeVault
@CodeVault 3 жыл бұрын
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.
@js4773
@js4773 3 жыл бұрын
@@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!
@CodeVault
@CodeVault 3 жыл бұрын
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
@js4773
@js4773 3 жыл бұрын
@@CodeVault Thanks, it would be great, if you go over pthread spinlock as well and compare the difference between mutex/spinlock. Thanks!
@ShubhamKumar-id9sm
@ShubhamKumar-id9sm 3 жыл бұрын
What does errno.h do?
@CodeVault
@CodeVault 3 жыл бұрын
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
@tumblelandproduction
@tumblelandproduction 3 жыл бұрын
If trylock == 0 that means it was not locked, so it locks itself then enters the critical section?
@CodeVault
@CodeVault 3 жыл бұрын
Yes, you can read more about this function on the docs page: linux.die.net/man/3/pthread_mutex_trylock
@NoneStopDamage
@NoneStopDamage 2 жыл бұрын
slow down man god damn
@CodeVault
@CodeVault 2 жыл бұрын
I think KZbin 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.
@timotheelegros1716
@timotheelegros1716 2 жыл бұрын
@@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.
@guillaumelacroix4436
@guillaumelacroix4436 Жыл бұрын
also agree, your speed is perfect! Thank you for your videos! @@CodeVault
@AlFredo-sx2yy
@AlFredo-sx2yy Жыл бұрын
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.
Condition variables in C
18:57
CodeVault
Рет қаралды 97 М.
why do void* pointers even exist?
8:17
Low Level
Рет қаралды 384 М.
😜 #aminkavitaminka #aminokka #аминкавитаминка
00:14
Аминка Витаминка
Рет қаралды 3,1 МЛН
这是自救的好办法 #路飞#海贼王
00:43
路飞与唐舞桐
Рет қаралды 126 МЛН
How Strong is Tin Foil? 💪
00:25
Brianna
Рет қаралды 68 МЛН
Deadlocks in C
10:59
CodeVault
Рет қаралды 27 М.
What is a mutex in C? (pthread_mutex)
9:18
CodeVault
Рет қаралды 168 М.
What are detached threads?
8:42
CodeVault
Рет қаралды 21 М.
Header Issues: Guards, Name Mangling, and extern "C"
8:32
Jacob Sorber
Рет қаралды 78 М.
Stupid C Tricks: Unsafe Functions You MUST Avoid!
9:26
Dave's Garage
Рет қаралды 62 М.
how Google writes gorgeous C++
7:40
Low Level
Рет қаралды 939 М.
Using read write locks (example in C)
17:11
Jacob Sorber
Рет қаралды 6 М.
What you should know about Threads in .NET
14:52
Raw Coding
Рет қаралды 7 М.
C# Async Await Mistakes | Part 1
10:19
Amichai Mantinband
Рет қаралды 36 М.
😜 #aminkavitaminka #aminokka #аминкавитаминка
00:14
Аминка Витаминка
Рет қаралды 3,1 МЛН