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...

КОМЕНТАРІ • 26

  • @LightLord1870
    @LightLord1870 3 роки тому +9

    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 роки тому +6

    Code vault coming in clutch again, thank you!

  • @oussamasallak9490
    @oussamasallak9490 2 роки тому +2

    Great explaination thank you

  • @jackh4158
    @jackh4158 Рік тому

    thanks for the explanation!

  • @andrelemospiucco5364
    @andrelemospiucco5364 29 днів тому

    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

  • @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 місяців тому +1

      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?

  • @narasarajv5278
    @narasarajv5278 3 роки тому +1

    Thank you for this video, I am watching all your videos. May i know your name please..

  • @stefanknezevic6362
    @stefanknezevic6362 2 роки тому +1

    Why is there no warning, for reaches end of non-void function (routine)?

    • @CodeVault
      @CodeVault  2 роки тому +1

      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

  • @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 роки тому +1

      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!

  • @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

  • @Codality
    @Codality 2 роки тому

    THX

  • @ShubhamKumar-id9sm
    @ShubhamKumar-id9sm 3 роки тому +1

    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

  • @NoneStopDamage
    @NoneStopDamage 2 роки тому +1

    slow down man god damn

    • @CodeVault
      @CodeVault  2 роки тому +7

      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.

    • @timotheelegros1716
      @timotheelegros1716 2 роки тому +1

      @@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 Рік тому +1

      also agree, your speed is perfect! Thank you for your videos! @@CodeVault

    • @AlFredo-sx2yy
      @AlFredo-sx2yy Рік тому +1

      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.