What is difference between Semaphore and Mutex

Поделиться
HTML-код
  • Опубликовано: 2 окт 2024
  • Mutex is a object owned by thread who is executing in critical section whereas semaphore is a signaling mechanism.

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

  • @nishantchauhan1548
    @nishantchauhan1548 5 лет назад +345

    Of all possible examples in the world, you chose a toilet...

  • @whatdoyousay4236
    @whatdoyousay4236 5 лет назад +42

    Although little weird, toilet is actually is a very good example for this! It's intuitive to understand that you "lock" it and that one person uses it at a time.

  • @zhengrui315
    @zhengrui315 4 года назад +27

    I really like this toilet, sorry i mean tutorial

    • @zhengrui315
      @zhengrui315 3 года назад +3

      @@blazkowicz666 seriously? you are picking a fight over this comment? I have no idea why it's offending to you

    • @blazkowicz666
      @blazkowicz666 3 года назад +3

      @@zhengrui315 I thought that was something Racist implied before I watched the video. I have deleted the comment 🤝

    • @UdayKumar-zm4ft
      @UdayKumar-zm4ft 3 года назад

      🤣🤣🤣

  • @MultiShiv19
    @MultiShiv19 7 лет назад +171

    Watch at 1.25x speed

  • @sent4dc
    @sent4dc 7 лет назад +113

    All I could understand is "toilet."

    • @PK-xe6sm
      @PK-xe6sm 6 лет назад +2

      A few times during this video I've been wondering if he did still use English

    • @unknownuser6143
      @unknownuser6143 6 лет назад

      sent4dc 😂😂😂😂

    • @nishik332
      @nishik332 5 лет назад

      Did u pass in the exam

    • @badger6431
      @badger6431 4 года назад

      Because you are full of s-hit.

  • @rishkum536
    @rishkum536 6 лет назад +18

    I am on my way to a Computer Science degree. I have a very good professor but you explained it way better and made it easier.

  • @ifxzrvz1706
    @ifxzrvz1706 4 года назад +3

    thank you, your explanation is one of the best.

  • @XEQTIONRZ
    @XEQTIONRZ 8 лет назад +59

    The only issue with your explanation is that when a mutex is locked, another thread trying to access the mutex doesn't get returned an error as you say. It just waits until the mutex is unlocked. I think you get it, just worded it improperly. Otherwise good video.

    • @muhammadwasif881
      @muhammadwasif881 7 лет назад

      In implementation Mutex generates busy wait and starvation so I think he is right..!

    • @muhammadwasif881
      @muhammadwasif881 7 лет назад +1

      to avoid this busy wait you need to do this... [s] is the flag/switch
      mov ax, 0
      xchg as, [s]
      comp ax, 0
      JNZ exit
      sleep
      exit

    • @wildglorypsn
      @wildglorypsn 7 лет назад

      not necessarily...starvation doesn't happen often.

    • @subhashishsen6157
      @subhashishsen6157 5 лет назад +1

      use mutex_try_lock() instead of mutex_lock()

    • @daisysunshine6606
      @daisysunshine6606 4 года назад

      thanks for the explanation i was confused

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

    "Let's say this person wants to use toilet facilities" =)))))))))) Nice tutorial, thanks

  • @totommymmy
    @totommymmy 5 лет назад +27

    Thank you so much! The toilet is such a good example for memorizing and understanding!

    • @pepe6666
      @pepe6666 5 лет назад +1

      it actually really is.

  • @pepe6666
    @pepe6666 5 лет назад +4

    thank you for explaining this in terms of people using the toilet. its a great example. i finally get that a semaphore is basically just an atomic integer and a waiting-mechanism really. speaking of which i need to go park a turd now. thanks very much and wish me well for my crap

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

    Every mutex is built on top of a semaphore with the addition of an atomic for the fast path. The fast path makes uncontended locking in userspace possible with a small number of instructions.

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

    Welp, I found a way to ignore both of those, here's a snippet from what I'm currently constructing to give you an idea:
    ```
    dint Send_AndWaitAll( THREAD *thread, OBJECT *object )
    {
    BRANCH *branch = GrabThreadBranch( thread );
    if ( !thread )
    return 0;
    if ( thread != &main_thread )
    {
    SIGNAL *signal = GrabThreadSignal(thread);
    OBJECT tmp = {0};
    bool redo = false;
    object->que = time(NULL);
    signal->i = *object;
    while ( 1 )
    {
    YieldOther();
    /* Check if the thread sent us a signal */
    if ( signal->o.que < object->que )
    continue;
    /* Check if the signal was the one we were expecting */
    if ( signal.o.sig != SIGNAL_CONT )
    {
    tmp = signal->o;
    redo = true;
    continue;
    }
    break;
    }
    if ( redo )
    {
    tmp.que = object->que;
    tmp.sig = SIGNAL_REDO;
    signal->i = tmp;
    }
    }
    Send_AndWaitAll( branch->Init, sig, object );
    return Send_AndWaitAll( branch->Next, sig, object );
    }
    ```

  •  8 лет назад +13

    Very Good.Keep it up :)

  • @baqtronic7704
    @baqtronic7704 5 лет назад +8

    Seems a good explanation, but I struggled very hard to understand this english... Subtitles would be very nice (not automatic...)

    • @daisysunshine6606
      @daisysunshine6606 4 года назад

      is english your second language? maybe thats why it was hard.

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

      yeah i couldn't understand it either. My professor used this video as required watching but im gonna search for something else I can understand.

  • @vivekthakkar5276
    @vivekthakkar5276 5 лет назад +4

    How can someone dislike this video?

    • @aizazulhaq4702
      @aizazulhaq4702 5 лет назад

      Yeah sure... cz there is a toilet in the video as an example.. duhhh

  • @majidsaeidi7760
    @majidsaeidi7760 4 месяца назад +1

    most clearly example to know mutex and semaphore.
    Thanks

  • @kevinfernandez2269
    @kevinfernandez2269 5 лет назад +4

    He probably went to a dollar general where you have to get a key from the register for the bathroom.

  • @TheLPfunnTV
    @TheLPfunnTV 3 года назад +21

    You might have just saved my exams grade! I understood the explanation with the metaphore so much better and it sticks. Thank you!

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

      don't think there's any saving if you call it a metaphore

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

      @@omargamal2148 good one!

  • @tanaygupta632
    @tanaygupta632 3 года назад +3

    I want difference between mutex and binary semaphore.

  • @Mohammed-fk9dj
    @Mohammed-fk9dj 6 лет назад +4

    4 identical keys for the toilet!!! what if somebody already in toilet and another person come and open the toilet! LOL

    • @petermako3174
      @petermako3174 5 лет назад +3

      maybe the locks work in a way, that if somebody inserts the key into the lock from the inside it prevents turning the other key from unlocking the door

    • @TT-ud5gf
      @TT-ud5gf 3 года назад

      This is the best question :)

  • @shrimatkapoor2200
    @shrimatkapoor2200 4 года назад +1

    I still don't understand if the keys are identical, couldn't two threads access the same object and cause a race condition?

  • @jeyko666
    @jeyko666 4 года назад +16

    10/10, indian enough that it was a good tutorial!

  • @victorzedwings
    @victorzedwings 5 лет назад +2

    *Toilets must be a big problem elsewhere....*

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

    What is the benefit of mutex over semaphore?

  • @SportsIncorporated
    @SportsIncorporated 6 лет назад +4

    Great explanation. Made it all simple. For someone without a computer science degree, me.

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

    Instead of calling the class "Mutex", how about Microsoft should have called the class "Toilet" instead LOL.

  • @sagarsharma-bp5we
    @sagarsharma-bp5we 4 года назад

    I think this difference is wrong, you have given example of counting semaphore , But we also have Binary Semaphore , Then what is difference bw binary semaphore and mutex.

  • @medhm2262
    @medhm2262 5 лет назад +3

    My frend its nice exemple it will stell in my memory for ever tanks

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

    Mutex is just a simple lock?

  • @Penguinsxz
    @Penguinsxz 8 лет назад +5

    Thanks for the awesome review!

  • @voipims
    @voipims 7 лет назад +4

    This video is half....people do have confusion between mutex and binary semaphore

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

    its only theoretical concept dear. totally confusing video :(. You need to explain it's main purpose why we have to use mutex and why we have to use semaphore

  • @2dapoint424
    @2dapoint424 3 года назад +1

    I had to go to the toilet after watching this video..

  • @payalsagar1808
    @payalsagar1808 4 года назад +2

    honestly thankyou so much for this perfect example☺.... the concept got cleared

  • @burhanmuhyiddin4372
    @burhanmuhyiddin4372 4 года назад +4

    Thank you very much :) I also confused about the difference between mutexes and semaphores and you explained the difference very clearly.

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

    I hope none of these waiting people will throw timeout exception into his pants. Deferring the IO operation is dangerous thing.

  • @teuton8363
    @teuton8363 7 лет назад

    Thanks for the video, but please buy a good microphone. If video quality is bad thats one thing but audio quality should always be good, otherwise people tune out quickly.

    • @HowTomakeitdone
      @HowTomakeitdone  7 лет назад +1

      thanks for your concern.
      I will improve audio definitely.

  • @echosystemd
    @echosystemd 8 лет назад +1

    Very good. But I think you need to explain why mutex use ownership while semaphore use signaling mechanism.

    • @HowTomakeitdone
      @HowTomakeitdone  8 лет назад +5

      +echo Mutex allow only one process/thread to access shared data because of that it need to lock that object. Whereas semaphore multiple processes can be allowed and in order to notify them for availability of data signaling mechanism is used.
      Welcome to channel.

  • @killstriker64
    @killstriker64 6 лет назад +2

    thanks a lot for this explanation! you are awesome!

  • @saadmanahmed860
    @saadmanahmed860 4 года назад +1

    very very tributary illustration. great video. the idea of mutex and semaphore really come to me translucid now

  • @seba_duran
    @seba_duran 8 лет назад +5

    thanks for the video, now I understand better

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

    your voice is not audible loudly so,you better give some time on it while making vidios

  • @thomassauve6156
    @thomassauve6156 4 года назад

    with no coding examples this video is absolutely useless.

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

    very useful...Thanks

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

    Very clear explanation. Thanks!

  • @Shifter21000
    @Shifter21000 8 лет назад +2

    Very good explanation! Crisp and clean .

  • @blockobutter
    @blockobutter 4 года назад

    My textbook had so much abstraction and bullshit beating-around the bush with fornal words that I didn't understand anything. All I needed was the toilet example, with multiple keys.

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

    i found the speaker went faster and faster near the end and f
    ound it hard to understand

  • @notoppland
    @notoppland Год назад +1

    Very clear and easy to follow explanation, thank you!

  • @kartikpodugu
    @kartikpodugu 11 месяцев назад

    I didn't understand clearly. Is there a specific reason we used 4 toilets and 4 keys for Semaphore example ?
    If we want to understand the difference, we need to have same no. of toilets for both mutex and semaphore ?
    Also, if 4 toilets are there, and you take the key to use toilet, then also it looks like there is ownership.
    What is the advantage if we have same kind of key for all 4 bathrooms ?
    Isn't semaphore example same as having 4 mutexes for 4 toilets ?
    I can't imagine what is the advantage of having same key for all 4 toilets,
    Please help me understand.

  • @a2zuser1
    @a2zuser1 5 лет назад +1

    Thank you very clear was useful

  • @aruprakshit7218
    @aruprakshit7218 2 месяца назад

    Very good explanation. Loved it.

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

    Watching this for tomorrow interview

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

    Appreciate for your explanation!

  • @rtm0076
    @rtm0076 4 года назад +1

    Best explanation ever

  • @kithenry
    @kithenry 11 месяцев назад

    sounds like homie is laughin at his example hehehe

  • @peimanfarshbaf7522
    @peimanfarshbaf7522 5 лет назад +1

    best explanation i have ever seen

  • @akhilkandibanda
    @akhilkandibanda 7 лет назад +1

    awesome toilet example, i clearly understood

  • @sureshkumarkaushik6109
    @sureshkumarkaushik6109 8 лет назад +2

    Thanks for this video and it helps us to understand the main concepts in best way. and easy to remember the conepts.

  • @suyashvishnoi6157
    @suyashvishnoi6157 7 лет назад +2

    Now i clearly understand. Thank you sir

  • @ameyzulkanthiwar9148
    @ameyzulkanthiwar9148 6 лет назад +1

    very nice expiation..Thank you..

  • @andresperez4698
    @andresperez4698 8 лет назад +2

    muchisimas gracias.. me quedo bien claro...gracias a ud

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

    There can't be a better explanation than this. I luckily happened to watch this as my first video while studying the mutex vs semaphore concepts and I can say that it took me only the duration of this video to understand the very core principle associated with this topic. Thank you.

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

    Every theoric book says these things; I'd like to find a modern c++11/14 implementation. In real pratice we can't code description of the ideas but what we need are implementations

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

    Still very good explanation that made me understand, thank you

  • @pupunjena1862
    @pupunjena1862 6 лет назад +4

    these toilets must have gold basins,commodes,taps and stuff that is why they got keys

  • @Teslacoil33
    @Teslacoil33 5 лет назад

    For the semaphore, if all the keys are identical what's to stop a person opening an occupied toilet door?

  • @shovon9412
    @shovon9412 5 лет назад +1

    Great video very clear

  • @jonathanazzopardipaul5048
    @jonathanazzopardipaul5048 4 года назад +1

    This was a great explanation, thanks!

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

    Very very nice explanation

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

    thankyou sir for this video

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

    Me thinking coding multiplayer is hard
    Multithreading has entered the chat😅

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

    5:43 is it comparing if it is equal to zero not less than ..?

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

    believe or not your bathroom analogy helped me understood and finished my homework! Thank you so much.

  • @professormariadiaz
    @professormariadiaz 5 лет назад +1

    You had me at 'toilet'

  • @arthurimona5901
    @arthurimona5901 5 лет назад

    Thanks buddy for the informative video!

  • @paulohanded28
    @paulohanded28 7 лет назад +1

    what software did you use to do the animation?

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

    Wonderful explanation. Thank you! :)

  • @AhmadKhanNaul1
    @AhmadKhanNaul1 4 года назад

    Very very well method

  • @harshapalanki5491
    @harshapalanki5491 4 года назад

    Is binary semaphore and mutex same???

  • @wilsonfarias9248
    @wilsonfarias9248 7 лет назад +1

    Thank you for the video!

  • @TL-fe9si
    @TL-fe9si 6 лет назад

    what about P(mutex)V(mutex)? Does the semaphore now have ownership issues?

  • @amarjeetverma6965
    @amarjeetverma6965 4 года назад

    Watch in 1.25x

  • @adharshmohan9249
    @adharshmohan9249 7 лет назад +1

    good explanation. Thanks..

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

    very well explained

  • @lmnefg121
    @lmnefg121 4 года назад

    dude, you are a genius to use the toilet and key as the metaphor here.

  • @rogerzheng2814
    @rogerzheng2814 5 лет назад

    So isn't mutex just a special case of a semaphore?

  • @ibknl1986
    @ibknl1986 4 года назад

    Good example.

  • @satyaprakashsahoo7772
    @satyaprakashsahoo7772 5 лет назад

    Actually binary semaphore is one type of mutex

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

    great Video!

  • @prithvisinh
    @prithvisinh 6 лет назад +1

    Excellent presentation.

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

    lots of toilets

  • @unknownuser6143
    @unknownuser6143 6 лет назад +1

    Good tutorial

  • @farruhhabibullaev5316
    @farruhhabibullaev5316 5 лет назад

    What? Toilet example is funny one, Good explanation...

  • @anikettiwari6885
    @anikettiwari6885 5 лет назад

    If two threads will try to access data with the help of semaphore at same time race condition can come. How can we avoid it

  • @Light-th1co
    @Light-th1co 6 лет назад

    how can I close a mutex in a process? Tell me please how can I do it in a batch file or per cmd.exe.

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

    Beautiful explanation! I always had trouble understanding semaphores intuitively until you provided the brilliant bathroom key analogy!!

  • @MinhTran-sl7kc
    @MinhTran-sl7kc 4 года назад

    Thank you Raja, I understand now.

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

    Thank U