What is difference between Semaphore and Mutex

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 พ.ย. 2024

ความคิดเห็น • 235

  • @whatdoyousay4236
    @whatdoyousay4236 5 ปีที่แล้ว +46

    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.

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

  • @uzairghole3025
    @uzairghole3025 7 หลายเดือนก่อน

    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.

  • @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 ปีที่แล้ว +7

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

    • @TheLPfunnTV
      @TheLPfunnTV ปีที่แล้ว

      @@omargamal2148 good one!

  • @nishantchauhan1548
    @nishantchauhan1548 5 ปีที่แล้ว +352

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

    • @weirdopersonnal
      @weirdopersonnal 5 ปีที่แล้ว +3

      hahahaha!

    • @justaninfiredperson
      @justaninfiredperson 5 ปีที่แล้ว +1

      Nishant Chauhan 😂

    • @Teslacoil33
      @Teslacoil33 5 ปีที่แล้ว +16

      Lock(mutex)
      "Do Work"
      Unlock(mutex)

    • @mariumahmed4567
      @mariumahmed4567 5 ปีที่แล้ว

      😂😂😂😂👏🏻👏🏻

    • @shovon9412
      @shovon9412 5 ปีที่แล้ว +30

      Of all examples the toilet will probably be most memorable

  • @totommymmy
    @totommymmy 5 ปีที่แล้ว +28

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

    • @pepe6666
      @pepe6666 5 ปีที่แล้ว +1

      it actually really is.

  • @camelcase9310
    @camelcase9310 4 ปีที่แล้ว

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

  • @MultiShiv19
    @MultiShiv19 8 ปีที่แล้ว +171

    Watch at 1.25x speed

    • @shivangikansal8025
      @shivangikansal8025 7 ปีที่แล้ว +1

      me too :D

    • @basantachaulagain6104
      @basantachaulagain6104 7 ปีที่แล้ว +6

      1.5x works fine for me

    • @Darieee
      @Darieee 7 ปีที่แล้ว +5

      I watched this at 20x guys .. I think I win

    • @omairbhore
      @omairbhore 7 ปีที่แล้ว +1

      I darieee you to watch it in 40x

    • @BloodHaZaRd666
      @BloodHaZaRd666 6 ปีที่แล้ว +6

      1.25 almost english, at 2 becomes japanese

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

  • @tedz2usa
    @tedz2usa ปีที่แล้ว

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

  • @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 5 ปีที่แล้ว

      thanks for the explanation i was confused

  • @majidsaeidi7760
    @majidsaeidi7760 5 หลายเดือนก่อน +1

    most clearly example to know mutex and semaphore.
    Thanks

  • @SportsIncorporated
    @SportsIncorporated 6 ปีที่แล้ว +4

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

  • @saadmanahmed860
    @saadmanahmed860 4 ปีที่แล้ว +1

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

  • @lmnefg121
    @lmnefg121 4 ปีที่แล้ว

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

  • @payalsagar1808
    @payalsagar1808 4 ปีที่แล้ว +2

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

  • @Shifter21000
    @Shifter21000 8 ปีที่แล้ว +2

    Very good explanation! Crisp and clean .

  • @burhanmuhyiddin4372
    @burhanmuhyiddin4372 5 ปีที่แล้ว +4

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

  • @ifxzrvz1706
    @ifxzrvz1706 4 ปีที่แล้ว +3

    thank you, your explanation is one of the best.

  • @aruprakshit7218
    @aruprakshit7218 3 หลายเดือนก่อน

    Very good explanation. Loved it.

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

  • @notoppland
    @notoppland ปีที่แล้ว +1

    Very clear and easy to follow explanation, thank you!

  • @dboothe7889
    @dboothe7889 3 ปีที่แล้ว

    Great work! I now understand the difference the night before my exam. Many thanks Sir!

  • @medhm2262
    @medhm2262 5 ปีที่แล้ว +3

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

  • @SriLakshmi3395
    @SriLakshmi3395 8 ปีที่แล้ว +3

    Thanks a billion times.....U have such an awseome talent of explaining the most difficult stuff in the easiest possible way.....
    Before watching ur videos ,I struggled with Computer science.... Ur channel & ur way of simple easy teaching increased my passion for Computer Science...I wish u all success & healthy happy life.. God bless u.. :-)

    • @HowTomakeitdone
      @HowTomakeitdone  8 ปีที่แล้ว

      Thanks for nice comment
      It's really great.

  • @OlliS71
    @OlliS71 10 หลายเดือนก่อน

    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.

  • @peimanfarshbaf7522
    @peimanfarshbaf7522 5 ปีที่แล้ว +1

    best explanation i have ever seen

  • @047gauravkumartiwari5
    @047gauravkumartiwari5 ปีที่แล้ว

    A clear and lucid explanation!

  • @pengxiaohan3371
    @pengxiaohan3371 3 ปีที่แล้ว +1

    Very clear explanation. Thanks!

  • @rtm0076
    @rtm0076 4 ปีที่แล้ว +1

    Best explanation ever

  • @xamogxusx
    @xamogxusx 5 ปีที่แล้ว +16

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

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

  • @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 );
    }
    ```

  • @suyashvishnoi6157
    @suyashvishnoi6157 7 ปีที่แล้ว +2

    Now i clearly understand. Thank you sir

  • @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 ปีที่แล้ว

      🤣🤣🤣

  • @TellaTrix
    @TellaTrix ปีที่แล้ว

    Appreciate for your explanation!

  • @kevinfernandez2269
    @kevinfernandez2269 5 ปีที่แล้ว +4

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

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

  • @Penguinsxz
    @Penguinsxz 8 ปีที่แล้ว +5

    Thanks for the awesome review!

  • @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 5 ปีที่แล้ว

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

    • @warrenpeterson5709
      @warrenpeterson5709 2 ปีที่แล้ว

      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.

  • @michaelcrainiciuc
    @michaelcrainiciuc ปีที่แล้ว

    Still very good explanation that made me understand, thank you

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

  • @bluesteel1
    @bluesteel1 4 ปีที่แล้ว

    Very very nice explanation

  •  8 ปีที่แล้ว +13

    Very Good.Keep it up :)

  • @shovon9412
    @shovon9412 5 ปีที่แล้ว +1

    Great video very clear

  • @CoreDump451
    @CoreDump451 ปีที่แล้ว

    Wonderful explanation. Thank you! :)

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

  • @marcosramirez2278
    @marcosramirez2278 24 วันที่ผ่านมา

    So basically, mutex is a way to assign keys to processes, and semaphore keeps track of how many keys are left?

  • @killstriker64
    @killstriker64 6 ปีที่แล้ว +2

    thanks a lot for this explanation! you are awesome!

  • @anilkinikar
    @anilkinikar 3 ปีที่แล้ว

    Very good explanation.

  • @fatemakhalfan6980
    @fatemakhalfan6980 3 ปีที่แล้ว +1

    very useful...Thanks

  • @Teslacoil33
    @Teslacoil33 5 ปีที่แล้ว

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

  • @Iamnoone56
    @Iamnoone56 2 ปีที่แล้ว

    bhut umdaa

  • @SubcribetoTin
    @SubcribetoTin 8 ปีที่แล้ว

    Thanks! Simple and straight to the point.

  • @gaborm4767
    @gaborm4767 3 ปีที่แล้ว +1

    What is the benefit of mutex over semaphore?

  • @kartikpodugu
    @kartikpodugu ปีที่แล้ว

    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.

  • @seba_duran
    @seba_duran 8 ปีที่แล้ว +5

    thanks for the video, now I understand better

  • @teuton8363
    @teuton8363 8 ปีที่แล้ว

    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  8 ปีที่แล้ว +1

      thanks for your concern.
      I will improve audio definitely.

  • @SoferPeOZN
    @SoferPeOZN 3 ปีที่แล้ว +2

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

  • @rajattandon5692
    @rajattandon5692 2 ปีที่แล้ว

    Great explanation

  • @jonathanazzopardipaul5048
    @jonathanazzopardipaul5048 4 ปีที่แล้ว +1

    This was a great explanation, thanks!

  • @ameyzulkanthiwar9148
    @ameyzulkanthiwar9148 6 ปีที่แล้ว +1

    very nice expiation..Thank you..

  • @bharat_india12
    @bharat_india12 2 ปีที่แล้ว

    Watching this for tomorrow interview

  • @amithstark8946
    @amithstark8946 10 หลายเดือนก่อน

    very well explained

  • @akhilkandibanda
    @akhilkandibanda 7 ปีที่แล้ว +1

    awesome toilet example, i clearly understood

  • @voipims
    @voipims 8 ปีที่แล้ว +4

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

  • @MinhTran-sl7kc
    @MinhTran-sl7kc 4 ปีที่แล้ว

    Thank you Raja, I understand now.

  • @prithvisinh
    @prithvisinh 6 ปีที่แล้ว +1

    Excellent presentation.

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

  • @TL-fe9si
    @TL-fe9si 6 ปีที่แล้ว

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

  • @a2zuser1
    @a2zuser1 5 ปีที่แล้ว +1

    Thank you very clear was useful

  • @abhirajtyagi9244
    @abhirajtyagi9244 ปีที่แล้ว

    thankyou sir for this video

  • @AhmadKhanNaul1
    @AhmadKhanNaul1 4 ปีที่แล้ว

    Very very well method

  • @tanaygupta632
    @tanaygupta632 3 ปีที่แล้ว +3

    I want difference between mutex and binary semaphore.

  • @matthiaswolf2288
    @matthiaswolf2288 ปีที่แล้ว

    great Video!

  • @farruhhabibullaev5316
    @farruhhabibullaev5316 5 ปีที่แล้ว

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

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

  • @rogerzheng2814
    @rogerzheng2814 5 ปีที่แล้ว

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

  • @enkr1
    @enkr1 ปีที่แล้ว

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

  • @andresperez4698
    @andresperez4698 8 ปีที่แล้ว +2

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

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

  • @harshapalanki5491
    @harshapalanki5491 4 ปีที่แล้ว

    Is binary semaphore and mutex same???

  • @arthurimona5901
    @arthurimona5901 5 ปีที่แล้ว

    Thanks buddy for the informative video!

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

  • @paulohanded28
    @paulohanded28 7 ปีที่แล้ว +1

    what software did you use to do the animation?

    • @HowTomakeitdone
      @HowTomakeitdone  7 ปีที่แล้ว

      This is simple PowerPoint presentation

  • @adharshmohan9249
    @adharshmohan9249 7 ปีที่แล้ว +1

    good explanation. Thanks..

  • @pavanisivadath9813
    @pavanisivadath9813 8 ปีที่แล้ว

    good work in making the pictures

  • @angelakarapetyan586
    @angelakarapetyan586 3 ปีที่แล้ว

    Mutex is just a simple lock?

  • @wilsonfarias9248
    @wilsonfarias9248 7 ปีที่แล้ว +1

    Thank you for the video!

  • @Light-th1co
    @Light-th1co 7 ปีที่แล้ว

    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.

  • @ibknl1986
    @ibknl1986 4 ปีที่แล้ว

    Good example.

  • @unknownuser6143
    @unknownuser6143 6 ปีที่แล้ว +1

    Good tutorial

  • @2dapoint424
    @2dapoint424 3 ปีที่แล้ว +1

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

  • @pupunjena1862
    @pupunjena1862 6 ปีที่แล้ว +4

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

  • @victorzedwings
    @victorzedwings 5 ปีที่แล้ว +2

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

  • @koti571bulasara4
    @koti571bulasara4 7 ปีที่แล้ว

    good explanation

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

  • @madnesscompilation2698
    @madnesscompilation2698 5 ปีที่แล้ว

    thank you 😉😉😉😉😉

  • @mjfryc
    @mjfryc 2 ปีที่แล้ว

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

  • @afraimlevi9871
    @afraimlevi9871 3 ปีที่แล้ว

    Thank U