Difference between Binary Semaphores and Mutexes

Поделиться
HTML-код
  • Опубликовано: 2 окт 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...

Комментарии • 49

  • @narasarajv5278
    @narasarajv5278 3 года назад +5

    Today I understood clearly with practical example.Thank you...

  • @wangstboss9232
    @wangstboss9232 Год назад +6

    Your explanation is really good and clear with code examples. Thank you so much!

  • @tukaramgaikwad1793
    @tukaramgaikwad1793 3 года назад

    Thank you for this video It cleared diff between Binary semaphore and Mutex

  • @bilalahmedkhan5876
    @bilalahmedkhan5876 2 года назад +5

    This channel is a life saver.

  • @animeshkumarsinha5954
    @animeshkumarsinha5954 3 года назад +5

    Concepts detail description are nice 🙂

  • @mikmik3965
    @mikmik3965 25 дней назад

    Undefined behavior almost cost me my OS HW thank god for resubmission

  • @bhrt1995
    @bhrt1995 3 года назад +1

    Great work.....👍👍👍
    Please do tutorials on GLib (Gnome)......
    If possible suggest tutorials.....

    • @CodeVault
      @CodeVault  3 года назад

      What exactly would you like to see? glib is quite a large library

  • @temmmbebkd2922
    @temmmbebkd2922 3 года назад +1

    It's been along time hope you are doig ok teacher 😇

  • @chetan788
    @chetan788 2 года назад +1

    Semaphore maintains a queue for all those threads that tried to enter the critical section when the semaphore variable is 0. So every thread gets a chance in the end in a fair manner (FIFO). While mutex doesn't provide such functionality, and randomly wakeup the thread. Is my statement correct?

    • @CodeVault
      @CodeVault  2 года назад

      There isn't any waiting queue mentioned in the documentation. The order in which threads at a semaphore continue is not defined

  • @backendengineering007
    @backendengineering007 8 месяцев назад

    QQ: First thread enters the routine1 and then takes the lock. Now unless this thread calls unlock on this lock, how can any other thread enter the critical section(as another thread has the lock and in the critical section)?? But, in your case I see a lot of 'Current value is:.." printed in the console. Am I missing something here?
    Guess: Is even this is a part of undefined behaviour?

    • @CodeVault
      @CodeVault  8 месяцев назад

      Yes that's undefined behavior. You're not supposed to lock and unlock mutexes from different threads. If you need that, just use semaphores

  • @johnduraisinghy7893
    @johnduraisinghy7893 3 года назад +1

    Hi,
    I have tried in my lab to unlock a mutex in a different thread and it was working completely fine for me.
    But when I create a mutex with type as PTHREAD_MUTEX_ERRORCHECK, unlocking in different thread throws error in return code.
    Now my question,
    1. How unlocking a normal(default init ) mutex in different thread works fine for me. Any possible reasons ?
    I tried with while (1) loop to lock in in thread and unlock in another thread for 2 hrs and did not see any issue in it.

    • @CodeVault
      @CodeVault  3 года назад +2

      "1. How unlocking a normal(default init ) mutex in different thread works fine for me. Any possible reasons ?"
      It's undefined behaviour. Meaning that there's a chance that, what you did right there, might work, but it's never guaranteed to work on every machine.

  • @1files
    @1files 6 месяцев назад

    nice explanation

  • @teddythots4644
    @teddythots4644 4 месяца назад

    you're goated

  • @Somali-iv9pu
    @Somali-iv9pu 6 месяцев назад

    Hey mate where have you been the last months. I wanted to thank you you videos are the best for c intermediate level learning plus i was wondering if you are planning on doing some socket programming in c for linux

    • @CodeVault
      @CodeVault  6 месяцев назад +1

      I have been busy, I will get back to making videos some day. Socket programming is on the top of my list

  • @user-hz7hp1dj1k
    @user-hz7hp1dj1k 6 месяцев назад

    Your explanation is so GREAT!
    Thank You & we will absolutely wait for new things to learn from you.

  • @nikhilagrawal9217
    @nikhilagrawal9217 9 месяцев назад

    Thank you so much for this video. It was very knowledgable.

  • @natiiim6336
    @natiiim6336 2 года назад +1

    could you make at least one video with an exercise where you combine the use of semaphores and mutexes? In my classes they tend to put problems like that in exams >_

    • @CodeVault
      @CodeVault  2 года назад +5

      This one does use both: code-vault.net/lesson/tlu0jq32v9:1609364042686
      But I will look into making more such videos!
      Think of semaphores as a way to control how many times a certain type of action can be executed.
      Mutexes are simply used to protect a part of memory from being accessed by multiple threads at the same time

  • @mikefdop2829
    @mikefdop2829 8 месяцев назад

    This is good. Thanks.

  • @kotravaijm250
    @kotravaijm250 2 года назад

    THANK YOU! for making this!

  • @chetan788
    @chetan788 2 года назад

    When the semaphore value is initialised to 0 and a thread called sem_post() thrice, then the value of the semaphore becomes 3? When initialised to 0, should it be called as binary semaphore or counting semaphore?

    • @CodeVault
      @CodeVault  2 года назад

      Really, it's however you want. For the semaphore API a binary semaphore is no different than a counting semaphore

  • @i_am_wiz
    @i_am_wiz Год назад

    Brilliant explanation with illustration

  • @utkarshtripathi2349
    @utkarshtripathi2349 3 года назад

    A very very thank you for this informative video

  • @benrogers5736
    @benrogers5736 2 года назад

    Great video! Thanks

  • @sasamilenkovic6053
    @sasamilenkovic6053 Год назад

    Excellent lesson. Brilliant teacher!

  • @prapulkrishna
    @prapulkrishna 3 года назад

    Thanks for video

  • @atulchoubey2215
    @atulchoubey2215 3 года назад

    Why did the code even executed for a while? It should have break in the beginning itself

    • @CodeVault
      @CodeVault  3 года назад

      Well... it's undefined behaviour. So an overflow probably occurred somewhere or something accessed memory that it shouldn't

  • @nobi_nose9469
    @nobi_nose9469 3 года назад

    thank you for making these!

  • @prashantgarg3013
    @prashantgarg3013 3 года назад

    siraaa veere

  • @shamanthhebbar528
    @shamanthhebbar528 3 года назад

    in case the semaphore value is greater than 1 and multiple threads are executing the critical section will it lead to a race condition?

    • @CodeVault
      @CodeVault  3 года назад

      Yes! That's why you might see both semaphores and mutexes used in the same context

  • @tripiper1362
    @tripiper1362 3 года назад

    but how you lock one thread function and then the other function can be executed if the first lock is not already unlocked?

    • @CodeVault
      @CodeVault  3 года назад +1

      That's the idea, you can't lock the mutex in one thread and unlock in the other

  • @alvarezfermin7948
    @alvarezfermin7948 3 года назад

    4:25

  • @dhruvshah7952
    @dhruvshah7952 2 года назад

    for some reason my mutex code is not breaking? don't know if this has to do with the new M1 mac ARM chips or not

    • @CodeVault
      @CodeVault  2 года назад

      That's interesting. I tested it and it doesn't break on my side either. Could be something MacOS specific although its documentation specifies that it should fail if the thread unlocking it didn't lock it: developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/pthread_mutex_unlock.3.html#//apple_ref/doc/man/3/pthread_mutex_unlock

    • @dhruvshah7952
      @dhruvshah7952 2 года назад

      @@CodeVault yeah I am pretty sure its the MacOs or the chip. There are actually quite a few things that does not happen when I am watchin your videos on multithreading, semaphores, etc when I code it on my machine

    • @CodeVault
      @CodeVault  2 года назад

      What are those exactly? I wanna to test them if possible

  • @virendrakumarmishra876
    @virendrakumarmishra876 3 года назад

    Sir Please create video on shared memory and Pls tell how to read particular length data From the shared memory, i went through everywhere like stack overflow etc didn't get my answer please explain..