Condition variables in C

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

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

  • @CARDSSerus
    @CARDSSerus 3 года назад +192

    I'm sure this gets said a lot, but this pthread playlist has legitimately helped me more than a semester of class. Thank you

  • @ArkFreestyle
    @ArkFreestyle 3 года назад +121

    Thank you so much, these are the highest quality C tutorials on youtube. Very underrated channel, you're an excellent teacher, please continue teaching!

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

      I dont mean to be offtopic but does someone know of a method to get back into an Instagram account?
      I was dumb lost my password. I appreciate any help you can offer me!

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

      @Trevor Joel instablaster :)

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

      @Samuel Lionel Thanks for your reply. I found the site through google and I'm waiting for the hacking stuff atm.
      Looks like it's gonna take a while so I will get back to you later with my results.

  • @mustafakarakas1116
    @mustafakarakas1116 2 года назад +15

    16:35 The difference between pthread_cond_signal() and pthread_cond_broadcast() lies in
    what happens if multiple threads are blocked in pthread_cond_wait(). With
    pthread_cond_signal(), we are simply guaranteed that at least one of the blocked
    threads is woken up; with pthread_cond_broadcast(), all blocked threads are woken up.

  • @ellenzeras
    @ellenzeras 19 дней назад

    4 years later and these videos are still helping a lot of students all around the world. thank you!!

  • @elenakalla
    @elenakalla 3 года назад +18

    I think you have the best teaching skills I've seen in all my school and University years. Thanks for educating us!

  • @benjangel
    @benjangel 6 месяцев назад +3

    I cannot describe my gratitude my man. Solid high quality teaching. You're being loved from Turkiye.

  • @playertwo9895
    @playertwo9895 3 года назад +8

    Sergiu, I can't thank you enough for this video. My OS professor really struggled to explain this topic, so I left the class without any intuition as to why condition variables were needed. Your example at 8:16 made my jaw drop, furthermore, your description of signalling being an indication that a condition's result *may* have changed really cleared things up for me.
    This is the second video of yours that I've watched (the first being pthread Barriers). It's safe to say that I'll be subscribing to your channel. Thank you for sharing your knowledge of these complex topics, you make them much easier to understand, and for that I'm grateful.

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

      I was looking at concurrency stuff again earlier today and remembered this video. Just wanted to say thanks again, I've since graduated and have been working at a big tech company :)

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

    I have been searching since yesterday on condition variables so i can understand what they are exactly and how they work, and i can say it's the most helpful thing! Cheers from Cyprus keep on the good work.

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

      lmao I am also from Cyprus are you studying on ucy?

  • @karangupta431
    @karangupta431 3 года назад +18

    My college teachers should teach like you , our lives would be fantastic then

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

    this is so so so good. usually, guys are speaking way fast because they don't understand that it's hard to follow the concepts for someone brand new being exposed to the subject. you really have a gift for teaching. People think just because they know a subject matter that they can become teachers but that's is not true. It takes special skills to be able to relate a material to someone else that is brand new to the matter at hand. Just a million thanks

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

    15:37 Thanks you for telling us about the implicit unlock and relock.

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

    Really glad I found this channel, the examples are always on point and a concept that seemed abstract previously makes perfect sense now!

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

    You have successfully explained something that my professors, articles online, a textbook, and documentation couldn't explain well. And concisely too! These videos are amazing, please make more!

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

    got your videos opened in one window and VS in another window working my CS 344 final assignment. My guy, you're the reason i'm passing.

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

    Thank you so much for these. You have no idea how much you have helped me with these C videos. God bless you.

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

    Wow well done! You did an amazing job explaining that. I love how you fast forwarded the more tedious coding we're already familiar with and really took your time with the nuances of condition variables

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

    Thanks a lot for explaining with an example. It really made it a lot easier to understand it's use and how to use it.

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

    best thread playlist s the best I could found in RUclips! Thanks !!

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

    Got a final in 2 days and this was an aspect of synchronization which has been confusing me all semester. Very helpful.

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

    This cleared up so much for me. Thank you very much for posting this!

  • @A.Safwat8
    @A.Safwat8 Год назад

    Thank You! I have been struggling with this whole concept and then found your video! Well explained! :)

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

    You're amazing thank you so much man. Your videos helped me tremendously with our OS and networking classes. Don't stop!

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

    Thank you, this was the only good explanation of condition variables i could find. Very well explained

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

    Brilliant and simple explanation, helped me to untangle my c++ condvars understanding. Many thanks!

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

    clear explanation with simple example, thank you for creating such helpful content

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

    Just great explanation! Can't wait till I got some time to watch your other content. :)

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

    Thanks a lot. It is hard to find people that understand stuff and can explain it properly to others

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

    Thank you for this video. It was presented very cleary and understandably.

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

    You're amazing. Thank you so much for share this knowledge.

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

    It is good for us to learn the concepts and implementation of the linux c!! Thank you!!

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

    Perfect! . Thank you for sharing your experiences.

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

    Thank you. You explained such a complex topic with so much ease. Thanks a lot.

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

    Thanks for this video. Your conceptual explanation is crystal clear !!

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

    The best video on this topic, best wishes for you!

  • @1guobro
    @1guobro 3 года назад

    Great video, helped me understand CV's from my OS course.

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

    Big brain explaination. thank you

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

    Great example and great explanation. Thanks!

  • @mohnish.deshpande
    @mohnish.deshpande Год назад

    Thank you so much for this content! Absolutely helpful.

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

    What a good explanation. Thank you!

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

    Excellent example, excellent content. Thank you.

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

    Thank you, it is usefully for my collage work.

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

    Pthreads concepts from 0 to master level. Please continue your contributions to this world. __/\__

  • @AndreFerreira-jv3qy
    @AndreFerreira-jv3qy 5 месяцев назад

    Great video man im learning so much from you thank you!

  • @Kingdomprogrammer
    @Kingdomprogrammer 12 дней назад

    I made a nice improvements to this code. maybe wanna have a look? I set a max size of the car's fuel tank and made sure the fuel tank was full before the car leaves the filling station and also made the the filling station only signal the car only when there's enough for the car(i.e fuel > 40) at least to help improve understanding

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

    Thank you very much for these videos, you have helped me a lot.

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

    Great explanation! Keep up the good work!

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

    Thank you, I hope it will help my in my exam tomorrow, I will let you know

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

      I passed, wonderful explanations, thank you

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

    Thank you!! Great help for my operating systems class

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

    Thank you so much for your explanation! It helped a lot.

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

    You are an awesome teacher! keep on these tutorials coming :))
    a question: is this the concept of producer/consumer? as I understand in this case, producer is the fuelFilling and the consumer is the car, right?

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

      I guess you can say that. Usually it has to do with entities in the sense that producers create entities that consumers have to process. Here you only have a number. Here's the video on the producer/consumer problem: code-vault.net/lesson/tlu0jq32v9:1609364042686

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

    Dude, you're amazing! Thank you so much!

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

    thank you so much for your videos, they're amazing!

  • @planetis-m6066
    @planetis-m6066 3 года назад +1

    Great video, thank you for sharing! I think it's better to unlock the mutex after signaling the condition variable. Again many thanks!

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

      I was actually curious on that topic and researched a bit. As it turns out, it doesn't really matter. You'd think that, because of the multithreaded nature of the program, there's a chance another thread would lock the mutex right after the unlock and right before the lock inside pthread_cond_wait. While it can happen, that's why we do a check every time we get past the pthread_cond_wait.
      Additionally, even if we have the order reversed (first signal then unlock), there's a chance another thread locks the mutex right after we call pthread_cond_signal. So either way, you can run in the same issue.
      Maybe you have another reason for it that I overlooked

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

    Man I really appreciate the help, you really know how to help

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

    Nice explanation Thank you...

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

    Thank you!! you did what my teacher couldn't

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

    Great Instructional Videos. Very helpful. Question: Could you have also solved the problem by putting the Mutex lock AFTER you check that fuel is above 40? Then the car would not lock the mutex unless there was enough fuel. With one car this would work.

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

      No. That would cause a race condition on the fuel variable. You have to lock the mutex before checking simply because some threads might try to write to that fuel variable in the meantime. So, you must make sure, no other threads are currently trying to write to it.

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

    Best video ever. Keep up the good work man

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

    when will the second part of this video be up? great explanation

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

      It's already up ;)
      You can check it out on our website: code-vault.net/lesson/18ec1942c2da46840693efe9b5203fac
      I don't usually name things "part 2" so that others searching for this topic will still find that video useful.

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

      @@CodeVault thanks alot

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

    thank you !! Great example!! Helpfull very helpfulllll

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

    Awesome explanation ! Thanks

  • @atTran-xv5iz
    @atTran-xv5iz Год назад

    I really hope you will make a similar series with Go

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

      The more I look into Go the more interesting it is. I might start learning it and then make some videos about it in the future

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

    Te la rifas mi estimado :33. Muchas gracias por tus videos, me estan ayudando mucho :'3

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

    I'll only say just one word: Perfect!

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

    Great content bro. You must be very smart to be able to come up with these great examples. Do you know where I can find some problems to practice this?

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

      I don't have a special website. But it's easy to come up with one. Just think about a "resource" that can be "used" by many and can be replenished by many others. That's basically it.

  • @Kingdomprogrammer
    @Kingdomprogrammer 12 дней назад

    in the fuel filling function, if you don't sleep after a single loop, it completes all 5 iterations before it signals the car routine thread. why's that so?? the signal is in the for loop. and since the code executes for each iterations.

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

    Plain and simple explaning, great job! Also i like the typing sound, can you share what keyboard and switches you use? Thanks

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

      Thanks! It's a Corsair K63 with Cherry MX reds. Although I don't recommend it, the key caps break very easily.

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

    Great explanation!

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

    Thank you for the video! One question, however, isn't it better for the call to pthread_cond_signal to be on a thread that owns the mutex, meaning calling pthread_cond_signal before unlocking? The man page recommends this for a more predictable scheduling. Thank you again.

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

      I actually researched this a bit and, from what I can tell, it really doesn't matter in this case because the the only thread that could unlock this mutex is the one we are signaling. In cases where you have some external thread that uses that mutex without the condition variable then, yes, it would be recommended to do so since the signaled thread would have a bigger chance of locking the mutex than the external ones. But all in all, it wouldn't impact the correctness of the code.

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

      @@CodeVault thank you for taking your time to respond, and for the response as well!

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

    Awesome explanation

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

    Great video! Thanks!

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

    Genius teaching!

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

    I 'cond' wait to start practising with pthread_cond.
    I'll be here all week folks. ;)

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

    very helpful, thank you very much :D

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

    I frickin love u

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

    Thanks for the great video. Quick question, shouldn't the condition variable be signalled and then the mutex be released? If there is priority inheritance due to holding the mutex, we still hold the elevated priority while signalling the condition variable but if we do release of mutex first, then the inherited priority would get lost and our thread may get pre-empted out before getting a chance to signal the condition variable.

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

      It was so long ago, I don't fully remember. I tested the difference between the two and, in practice, didn't see a difference

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

    I'm new to C, trying my best to learn the language to the fullest. But I'm having a hard time learning these concepts, I feel like I'm missing something. So if possible, can anyone please tell me of roadmap to C that I should follow?
    I appreciate the effort that went into this video.

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

      The concepts taught in this video are a bit more advanced. I suggest starting out with a simple pet project that you evolve over time. It could a simple calculator that uses the terminal for user input/output or some sort of simple game. Once you have worked a bit with these you can come back to these videos and possibly improve your project

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

      Thanks for the response, much appreciate it.

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

    Better quality than my university classes. -__-

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

    Thanks for the video! Clarified a few things for me, although i still have one thing that remains unclear: shouldn't the pthread_cond_signal() be called by the thread when it STILL HAS the lock on the MUTEX?....in your example you do it after UNLOCKING.

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

      The order doesn't really matter since pthread_cond_wait does call internally pthread_mutex_lock so it will wait for the mutex to be unlocked. AND if there are any external threads waiting on the lock then that order gives a better chance for that external thread to lock the mutex first (before the thread that called pthread_cond_wait). But this shouldn't change the correctness of your program.

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

    Thank you great job!

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

    You are the life changer

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

    amazing explanation

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

    9:40 why do we have to lock something when we are checking ? like Couldnt we put line 21 right after the while() loop?

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

      Because, even when reading a variable a race condition could occur. If one process would write and our process would read then, we would be reading old data. And, if by chance, the writing process writes to that variable right after checking it then we would encounter a race condition and our program would be doing something completely unexpected

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

    what an example !!🙂

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

    So helpful, thank you

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

    Very well done thank you !

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

    Nice perception.

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

    Is it possible to pass an array of function pointers, kinda like this thread is doing this series of jobs or before joining pass it another function?
    EDIT: I tried passing a struct thread_jobs with the job count and a pointer to a function pointer and initialized the struct with an array of function pointers made on the stack of main, passed the struct as an argument and it successfully serialized a series of jobs for each thread.
    ofc, the function pointer's return type and args are static but as a POC it works.

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

    Great tutorial

  • @אריאלצידון
    @אריאלצידון 2 года назад

    The best tracher i ever have..

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

    I couldńt find other videos, can You do a monitor implementation ? anyway Thankyou verymuch !

  • @jawwad4020
    @jawwad4020 Месяц назад

    You're the c++ boss!

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

    you are a life saver

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

    Classic consumer - producer problem, I would much rather see you solve it as an endless simulation (constantly fueling cars and when fuel too low in the main tank -> fill it up again;

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

    Very helpful !

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

    Thank you!

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

    I love you bro, are you a teacher ? You are explaining this better than mine

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

      No, I don't teach at any university or school. I just learned by helping a lot of people throughout the years

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

    God sends the Savior, He's the One

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

    THank you

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

    What about protecting the fuel integer from being cached in any of the cpu caches? I don't think a mutex is sufficient for that, and I'm told that volatile is not enough either.

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

      You don't have to worry about this issue. CPUs already handle synchronization primitives (such as semaphores, condition variables, mutexes ... ) in such a way that their cache will be consistent with memory. Whenever a write occurs to the fuel integer it invalidates all the other cores' caches for that variable's value