What is difference between Semaphore and Mutex

  Рет қаралды 472,037

HowTo

HowTo

8 жыл бұрын

Mutex is a object owned by thread who is executing in critical section whereas semaphore is a signaling mechanism.

Пікірлер: 232
@whatdoyousay4236
@whatdoyousay4236 5 жыл бұрын
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 5 жыл бұрын
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.
@totommymmy
@totommymmy 5 жыл бұрын
Thank you so much! The toilet is such a good example for memorizing and understanding!
@pepe6666
@pepe6666 4 жыл бұрын
it actually really is.
@uzairghole3025
@uzairghole3025 2 ай бұрын
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.
@burhanmuhyiddin4372
@burhanmuhyiddin4372 4 жыл бұрын
Thank you very much :) I also confused about the difference between mutexes and semaphores and you explained the difference very clearly.
@tedz2usa
@tedz2usa Жыл бұрын
Beautiful explanation! I always had trouble understanding semaphores intuitively until you provided the brilliant bathroom key analogy!!
@TheLPfunnTV
@TheLPfunnTV 2 жыл бұрын
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 Жыл бұрын
don't think there's any saving if you call it a metaphore
@TheLPfunnTV
@TheLPfunnTV Жыл бұрын
@@omargamal2148 good one!
@majidsaeidi7760
@majidsaeidi7760 27 күн бұрын
most clearly example to know mutex and semaphore. Thanks
@camelcase9310
@camelcase9310 3 жыл бұрын
believe or not your bathroom analogy helped me understood and finished my homework! Thank you so much.
@MultiShiv19
@MultiShiv19 7 жыл бұрын
Watch at 1.25x speed
@shivangikansal8025
@shivangikansal8025 7 жыл бұрын
me too :D
@basantachaulagain6104
@basantachaulagain6104 6 жыл бұрын
1.5x works fine for me
@Darieee
@Darieee 6 жыл бұрын
I watched this at 20x guys .. I think I win
@omairbhore
@omairbhore 6 жыл бұрын
I darieee you to watch it in 40x
@BloodHaZaRd666
@BloodHaZaRd666 5 жыл бұрын
1.25 almost english, at 2 becomes japanese
@SportsIncorporated
@SportsIncorporated 6 жыл бұрын
Great explanation. Made it all simple. For someone without a computer science degree, me.
@nishantchauhan1548
@nishantchauhan1548 5 жыл бұрын
Of all possible examples in the world, you chose a toilet...
@weirdopersonnal
@weirdopersonnal 5 жыл бұрын
hahahaha!
@justaninfiredperson
@justaninfiredperson 5 жыл бұрын
Nishant Chauhan 😂
@Teslacoil33
@Teslacoil33 4 жыл бұрын
Lock(mutex) "Do Work" Unlock(mutex)
@mariumahmed4567
@mariumahmed4567 4 жыл бұрын
😂😂😂😂👏🏻👏🏻
@shovon9412
@shovon9412 4 жыл бұрын
Of all examples the toilet will probably be most memorable
@notoppland
@notoppland Жыл бұрын
Very clear and easy to follow explanation, thank you!
@ifxzrvz1706
@ifxzrvz1706 4 жыл бұрын
thank you, your explanation is one of the best.
@saadmanahmed860
@saadmanahmed860 3 жыл бұрын
very very tributary illustration. great video. the idea of mutex and semaphore really come to me translucid now
@payalsagar1808
@payalsagar1808 4 жыл бұрын
honestly thankyou so much for this perfect example☺.... the concept got cleared
@dboothe7889
@dboothe7889 3 жыл бұрын
Great work! I now understand the difference the night before my exam. Many thanks Sir!
@SubcribetoTin
@SubcribetoTin 7 жыл бұрын
Thanks! Simple and straight to the point.
@HowTomakeitdone
@HowTomakeitdone 7 жыл бұрын
thanks welcome to the channel.
@lmnefg121
@lmnefg121 3 жыл бұрын
dude, you are a genius to use the toilet and key as the metaphor here.
@seba_duran
@seba_duran 7 жыл бұрын
thanks for the video, now I understand better
@HowTomakeitdone
@HowTomakeitdone 7 жыл бұрын
Welcome to the channel
@pepe6666
@pepe6666 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
@Shifter21000
@Shifter21000 7 жыл бұрын
Very good explanation! Crisp and clean .
@HowTomakeitdone
@HowTomakeitdone 7 жыл бұрын
thanks
@sureshkumarkaushik6109
@sureshkumarkaushik6109 7 жыл бұрын
Thanks for this video and it helps us to understand the main concepts in best way. and easy to remember the conepts.
@HowTomakeitdone
@HowTomakeitdone 7 жыл бұрын
Thanks Welcome to the channel.
@medhm2262
@medhm2262 5 жыл бұрын
My frend its nice exemple it will stell in my memory for ever tanks
@sent4dc
@sent4dc 7 жыл бұрын
All I could understand is "toilet."
@PK-xe6sm
@PK-xe6sm 6 жыл бұрын
A few times during this video I've been wondering if he did still use English
@unknownuser6143
@unknownuser6143 5 жыл бұрын
sent4dc 😂😂😂😂
@nishik332
@nishik332 5 жыл бұрын
Did u pass in the exam
@badger6431
@badger6431 3 жыл бұрын
Because you are full of s-hit.
@XEQTIONRZ
@XEQTIONRZ 7 жыл бұрын
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 жыл бұрын
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 жыл бұрын
use mutex_try_lock() instead of mutex_lock()
@daisysunshine6606
@daisysunshine6606 4 жыл бұрын
thanks for the explanation i was confused
@Penguinsxz
@Penguinsxz 8 жыл бұрын
Thanks for the awesome review!
@HowTomakeitdone
@HowTomakeitdone 8 жыл бұрын
welcome to the channel.
@jonathanazzopardipaul5048
@jonathanazzopardipaul5048 3 жыл бұрын
This was a great explanation, thanks!
@pengxiaohan3371
@pengxiaohan3371 2 жыл бұрын
Very clear explanation. Thanks!
@047gauravkumartiwari5
@047gauravkumartiwari5 Жыл бұрын
A clear and lucid explanation!
@arthurimona5901
@arthurimona5901 5 жыл бұрын
Thanks buddy for the informative video!
8 жыл бұрын
Very Good.Keep it up :)
@HowTomakeitdone
@HowTomakeitdone 8 жыл бұрын
Welcome to channel.
@sumitkaushik1942
@sumitkaushik1942 6 жыл бұрын
HowTo सपना
@andresperez4698
@andresperez4698 7 жыл бұрын
muchisimas gracias.. me quedo bien claro...gracias a ud
@suyashvishnoi6157
@suyashvishnoi6157 6 жыл бұрын
Now i clearly understand. Thank you sir
@HowTomakeitdone
@HowTomakeitdone 6 жыл бұрын
glad to know that :-)
@kevinfernandez2269
@kevinfernandez2269 5 жыл бұрын
He probably went to a dollar general where you have to get a key from the register for the bathroom.
@Teslacoil33
@Teslacoil33 4 жыл бұрын
For the semaphore, if all the keys are identical what's to stop a person opening an occupied toilet door?
@killstriker64
@killstriker64 5 жыл бұрын
thanks a lot for this explanation! you are awesome!
@HowTomakeitdone
@HowTomakeitdone 5 жыл бұрын
You are welcome.
@wilsonfarias9248
@wilsonfarias9248 6 жыл бұрын
Thank you for the video!
@kartikpodugu
@kartikpodugu 7 ай бұрын
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.
@TL-fe9si
@TL-fe9si 5 жыл бұрын
what about P(mutex)V(mutex)? Does the semaphore now have ownership issues?
@CoreDump451
@CoreDump451 Жыл бұрын
Wonderful explanation. Thank you! :)
@TellaTrix
@TellaTrix 11 ай бұрын
Appreciate for your explanation!
@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.
@shrimatkapoor2200
@shrimatkapoor2200 3 жыл бұрын
I still don't understand if the keys are identical, couldn't two threads access the same object and cause a race condition?
@OlliS71
@OlliS71 5 ай бұрын
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.
@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
@prithvisinh
@prithvisinh 6 жыл бұрын
Excellent presentation.
@paulohanded28
@paulohanded28 6 жыл бұрын
what software did you use to do the animation?
@HowTomakeitdone
@HowTomakeitdone 6 жыл бұрын
This is simple PowerPoint presentation
@ameyzulkanthiwar9148
@ameyzulkanthiwar9148 6 жыл бұрын
very nice expiation..Thank you..
@MinhTran-sl7kc
@MinhTran-sl7kc 4 жыл бұрын
Thank you Raja, I understand now.
@adharshmohan9249
@adharshmohan9249 6 жыл бұрын
good explanation. Thanks..
@wssz112
@wssz112 6 жыл бұрын
love that pictures!!!!! thank you
@peimanfarshbaf7522
@peimanfarshbaf7522 4 жыл бұрын
best explanation i have ever seen
@vivekthakkar5276
@vivekthakkar5276 5 жыл бұрын
How can someone dislike this video?
@aizazulhaq4702
@aizazulhaq4702 4 жыл бұрын
Yeah sure... cz there is a toilet in the video as an example.. duhhh
@zhengrui315
@zhengrui315 4 жыл бұрын
I really like this toilet, sorry i mean tutorial
@zhengrui315
@zhengrui315 3 жыл бұрын
@@blazkowicz666 seriously? you are picking a fight over this comment? I have no idea why it's offending to you
@blazkowicz666
@blazkowicz666 3 жыл бұрын
@@zhengrui315 I thought that was something Racist implied before I watched the video. I have deleted the comment 🤝
@UdayKumar-zm4ft
@UdayKumar-zm4ft 3 жыл бұрын
🤣🤣🤣
@akhilkandibanda
@akhilkandibanda 7 жыл бұрын
awesome toilet example, i clearly understood
@gaborm4767
@gaborm4767 2 жыл бұрын
What is the benefit of mutex over semaphore?
@jeyko666
@jeyko666 4 жыл бұрын
10/10, indian enough that it was a good tutorial!
@a2zuser1
@a2zuser1 5 жыл бұрын
Thank you very clear was useful
@enkr1
@enkr1 Жыл бұрын
5:43 is it comparing if it is equal to zero not less than ..?
@michaelcrainiciuc
@michaelcrainiciuc Жыл бұрын
Still very good explanation that made me understand, thank you
@shovon9412
@shovon9412 4 жыл бұрын
Great video very clear
@kuroketanItem
@kuroketanItem 8 жыл бұрын
thanks man , great example and explanation :thumb_up:
@HowTomakeitdone
@HowTomakeitdone 7 жыл бұрын
Thank you and welcome to the channel
@zxuiji
@zxuiji 2 жыл бұрын
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 ); } ```
@baqtronic7704
@baqtronic7704 5 жыл бұрын
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.
@anilkinikar
@anilkinikar 3 жыл бұрын
Very good explanation.
@mujjumaaz2008
@mujjumaaz2008 7 жыл бұрын
Easily understood.
@loinguyen.youtube
@loinguyen.youtube 7 жыл бұрын
Thank you thank you so much!
@HowTomakeitdone
@HowTomakeitdone 7 жыл бұрын
you are welcome.
@bluesteel1
@bluesteel1 3 жыл бұрын
Very very nice explanation
@amithstark8946
@amithstark8946 5 ай бұрын
very well explained
@bharat_india12
@bharat_india12 Жыл бұрын
Watching this for tomorrow interview
@jammy2003
@jammy2003 4 жыл бұрын
Thanks so much!
@rtm0076
@rtm0076 4 жыл бұрын
Best explanation ever
@abhirajtyagi9244
@abhirajtyagi9244 Жыл бұрын
thankyou sir for this video
@matthiaswolf2288
@matthiaswolf2288 Жыл бұрын
great Video!
@SriLakshmi3395
@SriLakshmi3395 7 жыл бұрын
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 7 жыл бұрын
Thanks for nice comment It's really great.
@unknownuser6143
@unknownuser6143 5 жыл бұрын
Good tutorial
@koti571bulasara4
@koti571bulasara4 7 жыл бұрын
good explanation
@pavanisivadath9813
@pavanisivadath9813 7 жыл бұрын
good work in making the pictures
@HowTomakeitdone
@HowTomakeitdone 7 жыл бұрын
thanks Welcome to the channel.
@rogerzheng2814
@rogerzheng2814 4 жыл бұрын
So isn't mutex just a special case of a semaphore?
@rajattandon5692
@rajattandon5692 2 жыл бұрын
Great explanation
@DEV-gz3nk
@DEV-gz3nk 6 жыл бұрын
what is mutex datatype
@fatemakhalfan6980
@fatemakhalfan6980 3 жыл бұрын
very useful...Thanks
@Iamnoone56
@Iamnoone56 2 жыл бұрын
bhut umdaa
@tanaygupta632
@tanaygupta632 3 жыл бұрын
I want difference between mutex and binary semaphore.
@harshapalanki5491
@harshapalanki5491 3 жыл бұрын
Is binary semaphore and mutex same???
@mywtfmp3
@mywtfmp3 8 жыл бұрын
Very good. But I think you need to explain why mutex use ownership while semaphore use signaling mechanism.
@HowTomakeitdone
@HowTomakeitdone 8 жыл бұрын
+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.
@AhmadKhanNaul1
@AhmadKhanNaul1 4 жыл бұрын
Very very well method
@poojasingh-nw3mv
@poojasingh-nw3mv 7 жыл бұрын
thanks for the video.....
@HowTomakeitdone
@HowTomakeitdone 7 жыл бұрын
Thanx for appreciating
@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
@voipims
@voipims 7 жыл бұрын
This video is half....people do have confusion between mutex and binary semaphore
@farruhhabibullaev5316
@farruhhabibullaev5316 5 жыл бұрын
What? Toilet example is funny one, Good explanation...
@afraimlevi9871
@afraimlevi9871 3 жыл бұрын
Thank U
@lavanyabellamkonda7399
@lavanyabellamkonda7399 7 жыл бұрын
nice video.thank you
@HowTomakeitdone
@HowTomakeitdone 7 жыл бұрын
thanks
@ibknl1986
@ibknl1986 4 жыл бұрын
Good example.
@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 жыл бұрын
thanks for your concern. I will improve audio definitely.
@KNT_19
@KNT_19 7 жыл бұрын
In case of semaphore, if the two threads accessing a shared resource, there will be race condition. Can tell, in this case, how synchronization is achieved?
@HowTomakeitdone
@HowTomakeitdone 7 жыл бұрын
If you want only one resource to be accessed at one time then you need to take semaphore value accordingly.
@gauravhiran6747
@gauravhiran6747 5 жыл бұрын
Use java "synchronised" keyword.
@ashishchauhan8494
@ashishchauhan8494 6 жыл бұрын
Thanks for video
@HowTomakeitdone
@HowTomakeitdone 6 жыл бұрын
you are welcome Ashish.
@blockobutter
@blockobutter 3 жыл бұрын
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.
@Mohammed-fk9dj
@Mohammed-fk9dj 6 жыл бұрын
4 identical keys for the toilet!!! what if somebody already in toilet and another person come and open the toilet! LOL
@petermako3174
@petermako3174 5 жыл бұрын
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 :)
What is a semaphore? How do they work? (Example in C)
13:27
Jacob Sorber
Рет қаралды 285 М.
Mutex VS Semaphore In C++
7:17
CppNuts
Рет қаралды 9 М.
I Built a Shelter House For myself and Сat🐱📦🏠
00:35
TooTool
Рет қаралды 29 МЛН
UFC 302 : Махачев VS Порье
02:54
Setanta Sports UFC
Рет қаралды 1,4 МЛН
ELE QUEBROU A TAÇA DE FUTEBOL
00:45
Matheus Kriwat
Рет қаралды 36 МЛН
Semaphore Vs. Mutex - A Clear Understanding
10:14
Shriram Vasudevan
Рет қаралды 97 М.
OS30 - Mutex Lock
9:53
EZCSE
Рет қаралды 16 М.
FANG Interview Question | Process vs Thread
3:51
ByteByteGo
Рет қаралды 278 М.
Semaphores
22:51
Neso Academy
Рет қаралды 437 М.
What is a mutex in C? (pthread_mutex)
9:18
CodeVault
Рет қаралды 155 М.
What's Spin Lock? Spin Lock Vs. Mutex.
5:07
Shriram Vasudevan
Рет қаралды 29 М.
Python Vs C++ Vs Java!
4:41
ByteByteGo
Рет қаралды 446 М.
Difference between Multiprocessing and Multithreading
4:07
codebasics
Рет қаралды 237 М.
Introduction to Java Semaphores - Java Programming
15:23
Will Tollefson
Рет қаралды 5 М.
I Built a Shelter House For myself and Сat🐱📦🏠
00:35
TooTool
Рет қаралды 29 МЛН