What is a mutex in C? (pthread_mutex)

Поделиться
HTML-код
  • Опубликовано: 1 янв 2025

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

  • @sergiomontes8606
    @sergiomontes8606 3 года назад +234

    This man deserves so many more views and subscribers. So well explained. My professors need to learn from him on how to teach.

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

    Give a man a fish and he'll eat for a day. Teach a man to fish and he'll eat every day.
    You are outstanding teacher mate. Your approach to keep these amazing videos free of cost makes you great!!! Amazing!!!!

  • @잠만보-k7s
    @잠만보-k7s 3 года назад +50

    This man is practically the best teacher I've ever had. Thank you SOOO MUCH.

  • @shanthgaitonde
    @shanthgaitonde 2 года назад +10

    The operations performed by a thread after acquiring a lock are called "critical section". This helps in achieving thread synchronisation. Thanks for the video!

  • @sagejpc1175
    @sagejpc1175 Год назад +2

    I want you to know you have taught me more about the C language in 10 minutes than an entire university lecture did in 80 minutes. I need to learn this for a project and you have made my life so much easier. Thank you so much.

  • @wtfitsdrewbritton
    @wtfitsdrewbritton 2 года назад +6

    Can’t thank you enough for your detailed and succinct explanations! When I began diving in to multithreaded programming with C++17 I became overwhelmed pretty quickly

  • @akapotatis9445
    @akapotatis9445 3 года назад +6

    This is something random but it's so attractive seeing someone being so smart

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

    Man, you are an absolute legend! Thank you so much for uploading this.

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

    BRO your tutorials bring me through my whole semester

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

    I wish you were one of my professors. Every time I watch a video from you I get excited to code again.

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

    dude you’re the best. Thank you so much for making these videos

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

    I bet you are not even close to understand how good you've done for humanity since you started this channel. Thank you very much.

  • @zakwanashfaq9879
    @zakwanashfaq9879 3 года назад +14

    you should train professors on how to teach
    Thanks for explaining, it was very easy to understand.

  • @PHTM04
    @PHTM04 Год назад +2

    Subscribed to your channel man. Its crazy how you break down everything into making them seem so easy! Much appreciated dude and I really hope more see your channel, much love!

  • @imqwerty5171
    @imqwerty5171 3 года назад +6

    Thanks for making these amazing tutorials. You earned a sub

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

    Man, you really make C look very simple and lovable.

  • @giladefrati8414
    @giladefrati8414 10 месяцев назад

    Thank you very much! You explained it so well and clearly; when my university lecturer explained it to me, I did not understand anything. I really like your channel.

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

    You saved alot of work and misunderstanding with the threads videos. Thanks alot

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

    This man is practically the best teacher! Sub!

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

    I purely watched this playlist to understand what the heck my OS prof was teaching when she taught us semaphores (although for processes) and I couldn't understand at that time. Loved it !

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

    You can tell how helpful his videos are because I have not seen 1 dislike yet. You make this so understandable

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

    You're an AMAZING teacher, Sir. Thanks for this. Subscribed!

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

    The lock variable + if block example is the best example I have even seen for this Mutex topic

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

    Hello. This is one of the greatest tutorials I've seen. I will be watching the whole playlist, thank you for your hard work and skill in education.

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

    thank you really , you are better than almost all the teachers in this world...
    all the respect

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

    You make complex things more easier, great job !! thank you.

  • @0xfsaymyname
    @0xfsaymyname 7 месяцев назад

    Man you are a God, thanks to sharing this with us!

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

    race cond can also happen in single core if interrupts are enabledand a variable is modified.

  • @ManaswiRaj-p8u
    @ManaswiRaj-p8u 10 месяцев назад

    Race conditions can also occur on single core processors like if we consider concurrent CPU and the operations are read, increment and write. So lets say P1 starts first then P1 reads increments and then it is preempted by another process P2, then P2 does read, increment and then it is preempted by P1. Now P1 writes the value and exits after which P2 comes and writes its value thus there is a race condition

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

      You're right, I didn't want to go into much detail regarding this and gave a simple (albeit wrong) explanation

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

    Just a slight correction at the end: You can have race conditions on single core processors. We can fake "parallel execution" by time slicing. If the two threads are sharing memory resources, then we can absolutely have a race condition during a context switch.

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

      That is correct.

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

    just a note mutuale exclusion problem can heppen even for single core processors , because when the quantum ends of a processoes , another process can modifie the variable

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

    Great video, explained how lock and unlock work really well. I was having trouble understanding what it was with my professors explanation lol. Thanks for explaining it, you definitely deserve more views and subs!

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

    This man is amazing! I wish he had merch, I would love to rock a code vault T-Shirt or Hoody

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

      Haha, thanks. I will think about launching some merch if enough people want them

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

    What a clear and simple explanation. Thanks my dude

  • @ndf782
    @ndf782 10 месяцев назад

    Your classes are amazing! All of them! Just one thing, I think that when you say race condition, the correct term is data race. Cheers!

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

    Great job. Explanations are easy and clear, very useful information. Thanks a lot!!!

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

    Thank you sir, these tutorials have made my synchronisation concepts very clear.

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

    PERFECT! looking forward to see a video about spinlock!

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

    many thanks for this series, sir! you'v saved me !!!!

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

    you are a gem! Thank you for the videos.

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

    Foarte bine explicat, mersi :)

  • @iloveukraine-subscribe1kgo822
    @iloveukraine-subscribe1kgo822 3 года назад +1

    Thank you for the practical Mutual Exclusion video.

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

    Thank you so much for the video!

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

    Best course on Multithreading!!

  • @Nat-qm5vb
    @Nat-qm5vb Год назад

    Once again, RUclips saves my homework. Thank you!

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

    Thank you so much for the explanation on Pthread mutex!

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

    Its incredible, I'm French but I understood perfectly this video, thanks you

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

    Ty for the awesome videos!

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

    I love your videos. Your materials are helping me a lot more than my lecturers do and also you making it simple and more understable. Looking forward to POSIX/ SYS V semaphores. Also I would like to ask will there be coming more materials about pthreads ? ( maybe private data of threads). Best of regards code vault. Keep it up!

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

      Thank you! And yes, the last lessons in the course are not yet decided so will add most of the suggestion from the community there (like this one ;) ).

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

    Thanks for the great explanation!

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

    Really a wonderful explanation. Thank you!

  • @jong.4864
    @jong.4864 2 года назад

    Thank you, you're so helpful!

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

    2:09 what if the threads met a race condition at reading lock?

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

      locks are thread-safe at the OS level

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

    You are a gem 🙏🏻

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

    I really enjoy your videos, thank you
    Could you make a video about Condition Variables with threads?

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

    You explain way more clear than my professor Park

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

    Very useful video! Thank you!

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

    Thank you sir , I appreciate your help !

  • @Dave-cq6pu
    @Dave-cq6pu 4 года назад

    Really useful videos, just as im learning this at school :D

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

    thank you for this playlist

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

    Fantastic explanation!!! Thanks

  • @cindaellas
    @cindaellas 5 месяцев назад

    great video! thanks!!!! very good job! greetings from germany

  • @Smile-si8xv
    @Smile-si8xv 8 месяцев назад +1

    arrayyy u r great ji😍

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

    This is beyond helpful! Thank you so much :)

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

    Thanks you so much you teaching method are soooo good.

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

    Great tutorial.
    I have one comment about the likelihood of race condition happening for single-core processors. Even with a single core and a single processor but multiple threads (as it is the case for the FreeRTOS OS in embedded systems), race conditions will happen more often than not without a mutex, atomic instructions, etc.

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

      Thanks! Interesting. I wonder now about single-core single-thread environments. Technically there still should be some race conditions but they might be encountered less often. Would you like to test that?

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

      @@CodeVault
      I think the only possibility for race conditions to happen for single-core single-threaded app, is when a hardware interrupt occurs and ISR gets executed.
      If hardware interrupts were not present, I'm not sure race conditions can ever occur for a single-core, single-threaded app

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

    AMAZING TEACHER!!!!!!!!!!!!!!!!!! THANK YOU!!!

  • @Alexander_444.20
    @Alexander_444.20 3 года назад

    great explanation thank you!

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

    God send savior, HE's the ONE

  • @Jonathan-ru9zl
    @Jonathan-ru9zl Год назад

    amazing explanation.

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

    Your video is the best. thx

  • @viniciuss.9250
    @viniciuss.9250 2 года назад

    Thank you SOOO MUCH.

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

    Love your Videos

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

    Good stuff my friend

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

    i comment for the referencement, you're a good guy

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

    Real time example super explanation.

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

    Thank you for your great video bro. You save my life !!!
    Just curious a little bit from the last video. Base on my understanding, Race condition happen when 2 thread read memory at the same time. Is that possible if mutex lock data in exactly same time and race condition happen. If not why?

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

      First things first: a race condition happens only in two cases:
      1) You have one thread writing a piece of data while other threads are reading it
      2) You have multiple threads writing the same piece of data
      Two threads reading the same piece of data won't cause a race condition.
      Although with mutexes a read and write could happen at the same time, they are designed so that each read and write on them is atomic, meaning there's no way other threads could read the data while another is in the process of writing it (only before it wrote to it or after that)

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

    Apparently there's another way of initializing a pthread_mutex:
    pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
    I wonder what the difference is

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

      They are basically the same. The first one could technically be faster since it only assigns some values but can only be used. Also, it guarantees that the lock is always initialized

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

    good explanation!

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

    Race codnitions can very well occur in single core processors too. Think of pre-emption.

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

    man, i wanna be your thread. such a good explanation

  • @DineshKumar-wo4tj
    @DineshKumar-wo4tj 2 года назад +1

    Even if I comment mutex_init and mutex_destroy, i can able to see mutex lock/unlock is working. Then what is the use of init/destroy function???

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

      Well, init is surely needed when using a mutex. Although, probably, since the mutex is global in this video it gets its members automatically initialized to 0 and I think that coincides with what pthread_mutex_init sets the values to. If you try to use the a local mutex I think you'd have issues without the init function call. The pthread_mutex_destroy is not 100% necessary on some architectures but it's good practice

    • @DineshKumar-wo4tj
      @DineshKumar-wo4tj 2 года назад +1

      @@CodeVault Thank you for your clarity.

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

    Could you make a video about semaphores and pointing the differences between it and mutexes' use cases?

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

      Yep, semaphores and barriers are also planned for this course

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

    Thank you.

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

    Bro small problem with your code.I took this as a refresher to threads.Locking and unlocking the mutex in each iteration of the loop introduces unnecessary overhead and reduces the potential benefits of multithreading. A better way is to lock and unlock it outside the loop.Anyway thanks for the video bro

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

      Of course. But this was a short explanation and I wanted to show that the mutexes work even if multiple loops are executing at the same time. In a production environment, this would be very inefficient indeed

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

    Thank you

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

    Thank you! :)

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

    Bravo ! thanks for explaining
    my question is : when we may need to create 2 threads with same function?

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

      Whenever you want to split CPU intensive work between the threads, that's when the workload is exactly the same except for some indices.

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

    Thanks, for the amazing tutorial. Keep posting the content :)
    Could you please post one video to print odd-even numbers in a sequence, so it will be more clear regarding the mutex and condition variables?

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

    Great work

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

    Can we say that mutex locking forces the multithreading routine into serial?

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

      Yes. Basically the critical section that is surrounded by a mutex lock/unlock will always be serially executed

  • @basic-1337
    @basic-1337 10 месяцев назад

    Greate video that makes me want to migrate from windows to linux😂

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

      For development it's definitely a must. Either to Linux or Mac... Windows is not great for development

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

    Maybe a minor misstatement when you say race conditions only happen on multicore systems. I think race conditions can also happen on one core processors. You could save your mails in thread 1 context switch to thread 2 and finish loop then go back to 1 and finish, then your result would only be 1,000,000 on write back. Great content though. Love this playlist.

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

      Yes, it's definitely wrong. Race conditions can happen on any system. It's just much much rare to happen on single-core processors as the context switch would have to happen right between an assignment (which, from what I recall, is a very low chance). Maybe I will make a video on this topic to investigate what is the chance of that happening and whatnot

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

      @@CodeVault that would be awesome 👍🏻

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

    good explanation bro

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

    you are a god

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

    Thanks for the wonderful video. I have a question. What happens if thread 1 throws an error before the unlock? Will thread 2 wait forever?

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

      The lock is released, so thread 2 won't wait forever

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

    Your a gem!

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

    Could you please make a video about marge, selection, bubble and quick sort in c?

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

      Will look into it... may come at a later time tho

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

      @@CodeVault thanks!

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

    I think using lock the execution of the program would be slower, but that is not important . More important is avoiding race condition

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

      Yes but doesn't that mean we're forcing multithreading program into serial program?

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

    I believe it’d be fair to at least mention that there are other options to approach this. Eg in this example atomic would work better.

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

      You mean atomic operations?

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

      @@CodeVault yep, like using atomic_int eg