Signaling for condition variables (pthread_cond_signal vs pthread_cond_broadcast)

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 ก.ย. 2024
  • Source code can be found here:
    code-vault.net...
    ===== Support us through our store =====
    code-vault.net...
    ===== Check out our website =====
    code-vault.net
    ===== Check out our Discord server =====
    discord.code-v...

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

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

    Your videos are improving my thinking ability.Thank you ...

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

    Thank you, I have found this, and the previous video on conditional variables extremely helpful. My lecture slides are too fast forward but you explain the concepts in a decent depth and speed.
    P.s. your speech is very clear which is also nice :)

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

    Fantastic video to learn such a complex concept.

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

    misto explicat, exact asta fac la facultate. esti un ajutor de nadejde in sesiune.

  • @user-yz7ts2fq9m
    @user-yz7ts2fq9m 3 ปีที่แล้ว +1

    This is incredible what you are doing now!

  • @user-hb2ex3wg9k
    @user-hb2ex3wg9k 3 หลายเดือนก่อน

    Thank you very much for this helpful tutorial

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

    your video is a life saver!! i couldn't understand what my professor lectures in class. But i'm totally with your logic. thank you so much. now i can start working on my homework now

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

    Thank you very much for these excellent quality videos, I have learned a great deal on conditinal variables to start my hw :)

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

    Thank you endlessly.

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

    Thank you so much sir, your videos are very helpful

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

    Great video!! Really helped me to understand the subject better.

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

    it's very useful for me, thank you so much.

  • @Dave-cq6pu
    @Dave-cq6pu 3 ปีที่แล้ว

    Very helpful and quality videos.

  • @saularaya2066
    @saularaya2066 3 หลายเดือนก่อน +1

    Piola👍

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

    Brilliant explanation.

  • @fabiozucconi7068
    @fabiozucconi7068 6 หลายเดือนก่อน

    just a simple checking correction: running the code through valgrind --tools=helgrind i found a possible problem in running the broadcast outside of the lock, simply solvable unlocking the mutex after running pthread_cond_broadcast.
    Hope this is useful =)

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

    Very helpful thanks 👍

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

    Thank you!

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

    thank you

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

    thank you for your amazing and helpful videos. I am wondering how in last example 2 cars are in lock? isn't the concept of mutex that only one thread in lock situation and others are waiting?

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

      Yep, you're right. The trick is with the pthread_cond_wait call. Behind the scenes it actually does the following:
      pthread_mutex_unlock
      wait_for_signal
      pthread_mutex_lock
      And because of that pthread_mutex_unlock that pthread_cond_wait executes other threads are allowed to lock that mutex. This is why you actually have to pass the mutex to pthread_cond_wait

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

      since feuler has broadcasted, all the four car threads are signalled to check the while loop but one car at a time since the car locks the mutex and then the second car comes and so on
      correct me if I'm wrong

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

      @@CodeVault if on waiting for signal it unlock then 2 case other car can take lock or fuel_filling
      if other car take lock then program will hault and if fuel_filling will take lock then it will emit signal cond_wait will receive and it will start execution or on recieving signal before taking lock other car can take lock but in any case only one car or fuel_filling can only take lock ??? can u explain this properly

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

    So the Filled fuel ... X is the amount of fuel in the car that gets filled from the fuel tank
    and the Now left: X is the amount of fuel in the fuel tank that gets filled from somewhere

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

    pthread_cond_signal vs pthread_cond_broadcast seem like great tools depending on your applications. I'm assuming broadcast is mainly used for times when multiple threads are accessing one shared pool/resource and for a threaded pipeline you'd stick with signal?
    I'm wondering due to a coding assignment for a pipeline that consisted of 4 threads and I assumed 3 mutexes. (read from file > parse characters and process one pattern > parse characters and parse another pattern > output exactly 80 chars from buffer. With a mutex between each part to keep them from speeding ahead) I had assumed only 3 mutexes each between the arrows, but I'm thinking I would need another mutex at the beginning to keep the reading thread from emptying the entire file and then closing the program early.

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

      Usually you use signal when you expect just one thread to start working (in a thread pool for example, when adding a new job to the list). You use broadcast when there's a possibility that the work requires more than 1 thread

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

    awesome

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

    01:41 Not fully sure, but it seems like it only awakens 1 car. Note how you initialized 4 cars, and there's only three initializing car prints above the first fueling. I'm guessing that the fueling function printed faster than the slowest car function instead of the conditional signal awakening 2 cars.

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

      Note that the car function only executes once. After the car has been fueled the thread finishes its execution (thus, for the next iteration, we only get 3 messages)

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

    Small question, in the filling_fuel function, should the broadcast/signal be done inside the mutex lock? I think i remember reading somewhere that it was best practice, or does it not matter?

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

      In this specific program it doesn't matter. If you use the mutex standalone (i.e. in a context that doesn't depend on this condition variable)... it will matter when it comes to priority.
      If the order is: Signal -> Unlock
      Then both the standalone and the pthread_cond_wait threads have basically the same time to lock the mutex.
      If the order is: Unlock -> Signal
      Then that standalone mutex has a biiit more time to lock the mutex (right before that pthread_cond_signal is executed) to lock the mutex.
      All in all, this priority shouldn't affect your program's correctness, but it's something to keep in mind

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

      @@CodeVault Makes sense, thank you!

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

    If one of the car thread does not get enough fuel and all the fuel-filling iterations are over, how can the waiting thread on the condition be terminated. Is there any giveup/retry logic that can be added for such waiting threads.

    • @CodeVault
      @CodeVault  9 หลายเดือนก่อน

      You could add a separate thread that simply checks for activity and have the cars always write to some variable the times it started trying to get fuel and got fuel

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

    Just wondering why in the last example, after filled 30, not 4 thread goes in (2 first time and 2 signaled)

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

      That is because the car threads were able to lock the mutex first due to the broadcast (to check once) before letting the other fuel thread execute

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

    Would there be any way to use signals to get the result you get with broadcast? I ask because I'm working on a programming assignment with threads and the professor has told us that we can only use wait and signal. We cannot use broadcast.

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

      I don't think there is an easy way without having a counter of how many threads are waiting and simply call pthread_cond_signal in a for loop. Although... usually, you only need to use signal, not broadcast, what is the reason you absolutely need broadcast?

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

    i have one doubt from start out of 4 car only 1 car can have lock so if 1st car take lock and waiting fro cond_wait and its unlock before lock taken by fuel_filling if another car take lock then fuel_filling will also wait for lock then what will happen??

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

      When calling cond_wait the mutex actually gets unlocked so others can access it. The thread relocks the mutex once signal or broadcast is called

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

      @@CodeVault i was saying like out of 4 car only 1 will be able to reach cond_wait condition right ? bcoz of lock
      n if it unlock before lock taken by fuel_filling does another thread can also take know is this possible?

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

      No. All of them will, simply because that, when waiting at pthread_cond_wait, the lock gets dropped and other threads can also lock and call pthread_cond_wait

  • @HarshVerma-oz5uo
    @HarshVerma-oz5uo 3 ปีที่แล้ว

    How to use conditional variable in detachable threads??

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

      Just like in any other threads. Detachable threads are just non-joinable, but they still share the memory so they still can make use of the tools you use in non-detachable threads

    • @HarshVerma-oz5uo
      @HarshVerma-oz5uo 3 ปีที่แล้ว

      @@CodeVault I have run the code successfully now. Thank you for your support

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

    what happens if signal is executed before another thread reaches condition wait? is the condition wait executed or does it wait until another signal is executed while it's waiting?

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

      It should wait until another signal gets executed. Here are the docs: linux.die.net/man/3/pthread_cond_signal

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

      @@CodeVault thank you, uni slides were iffy about it, btw I think these tutorials are top 1%

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

    Could you also make a video on pthread_cond_timedwait().
    Also could you help to cover scenario like:
    Thread2 goes in condition timed wait.
    Thread 1 takes mutex lock
    Thread2 pthread_cond_timedout is signalled due to timeout.
    What would happen in this scenario?
    As thread1 was still under mutex lock and now thread 2 due to timeout also is now under mutex lock.
    Is it a deadlock scenario?