Condition Variable in Modern cpp and unique lock | Introduction to Concurrency in C++

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

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

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

    Thanks for this, from this base I improved the code by adding infinite loops and scoped locks so that they communicate indefinitely, which is more a common used case

  • @not_ever
    @not_ever 11 месяцев назад +5

    I think there is a bug here. if(!notified) should be while(!notified) in case of spurious wakeups.
    As it said in the cppreference documentation for wait, the predicate version of wait is equivalent to:
    while (!stop_waiting())
    {
    wait(lock);
    }
    Have I misunderstood something ? It's quite likely I have, as I haven't written multithreaded C++ for a couple of years and I'm watching these videos to refresh my memory.

    • @MikeShah
      @MikeShah  11 месяцев назад +1

      Agreed, I think this is a bug in the video and my Code. This I think has been updated on cppreference as well (it's possible my interpretation was a bug as well)

  • @barbuceanu2005
    @barbuceanu2005 3 месяца назад +1

    After notify_one, the reporter thread tries to re-aquire the lock, but it is already locked by the worker thread (until the lock goes out of scope). If the lock would not go out of scope in the worker thread, a lock.unlock() would be needed before (or after) notify_one(), in order to allow the reporter thread to re-aquire the lock after wait, otherwise the reporter thread would remain blocked.

  • @VardaanNarang
    @VardaanNarang 5 месяцев назад +1

    Hi Mike! The series is great. My only request would be to create a video on std::promise.

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

      Cheers! Check out this video on futures: ruclips.net/video/4twJD5ezkag/видео.html that may be helpful!

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

    Hi Mike , this example similar to many examples out there , i think the idea of conditional variable is where the thread not finished , and have while (true) or while (m_running) than you need to use the unlock and lock of the unique lock, so this example need to be advanced little more.

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

      I suppose an example using say a producer/consumer where the producers and consumers have infinite loops waiting for items to be placed on a queue (or spaces available on the queue for a producer) would be another good example to use conditional variables :)

  • @DanteGordon-r2t
    @DanteGordon-r2t 11 месяцев назад +1

    Thank you this video helped me understand

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

    thanks for making those videos!

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

      You are most welcome! A few more trickling into this series in the next few weeks :)

  • @vb9950
    @vb9950 4 дня назад +1

    It's very good series, just wondering why didn't you go through semaphores but only mutexes.

    • @MikeShah
      @MikeShah  4 дня назад

      Cheers! Just need more time to plan and record :)

    • @vb9950
      @vb9950 3 дня назад +1

      @@MikeShah wow, never expected you to reply to all the comments. Are semaphores necessary, are there any real-time use cases for it?

    • @MikeShah
      @MikeShah  3 дня назад

      @@vb9950 Semaphores are one of the lowest level synchronization primitives. They can be used as barriers, used like mutexes, etc., so many use cases for them.

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

    I enjoyed this video, thank you for making it.

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

      You are most welcome!

  • @kowalski2031
    @kowalski2031 11 месяцев назад +2

    the notified variable seems useless in this code

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

      It seems like a slight optimization that eliminates the need to wait if the second thread has already finished.

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

    Thanks for the tutorial video, Mike.
    Feedback on the example program: I ran into the issue of "lost wakeup" with condition variables while trying out this.

  • @macewindont9922
    @macewindont9922 3 месяца назад +1

    Wouldn't the code be simpler if you eliminate the `notified` boolean? Can't you just always let the worker signal to the reporter thread, instead of filtering it with the boolean?

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

      We do need some variable to store state for condidtional variables, just one of the pieces needed.

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

    Hi MIke, this seems to work without the notified boolean variable. Although does that mean the reporter thread keeps on polling our conditional variable?

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

      Yes, I think some folks have noted that below.

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

    Hello Mike! Do you think you could do a video on the concept of Semaphores? It was recently added in C++20, see std::counting_semaphore, std::binary_semaphore, but I'm having a hard time understanding the use cases, and what exactly is the difference between a Semaphore and a conditional variable (when should you use one and not the other). Thanks!

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

      Yes, will add those to the series. Trying to wrap up a few other video series, but will otherwise revisit the concurrency -- I've started drafting some notes already, not sure on exact ETA yet for new lessons, likely around Fall

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

    Thanks Mike for this great series!
    I have a question:
    If a thread that is blocked on a std::condition_variable is notified, but the lock on the associated mutex has not been released and let’s say the lock is going to be released 10 seconds later, would the waiting thread wait for the lock to be released or the situation would be undefined?

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

      Should remain blocked on the lock if I understand the question correctly: en.cppreference.com/w/cpp/thread/condition_variable condition_variable needs a unique_lock

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

      @@MikeShah Thank you.

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

    Just as when I got 2 threads hung, your video shows up 😀. Can you think of a case where 2 mutexes might be needed in your example. I ask that, as my worker thread calls lock via unique lock, as the reporter thread starts and takes the lock, it seems lock is called on a locked mutex, which is undefined behavior. The worker lock call followed by cv wait, so if predicate is false the mutex will be unlocked for reporter to grab it. So could the case where worker condition is met so lock is held, meantime report tries to grab is resulting in UB. I am reluctant to use 2 mutex solutions, it makes no sense, here mutex with cv is used to protect shared data, can done with 1 mutex. But how to prevent locking a locked mutex. Thanks Mike

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

      Two mutexes tend to show up in patterns like producer/consumer or readers/writers problems. If you have some intermediate data structure where data is being stored you might want to use some condition variable to flag when data is ready for instance.

  • @Popart-xh2fd
    @Popart-xh2fd 6 месяцев назад

    What happens if the thread_worker finishes without notify_one?

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

    shouldn't the `result` and `notified` variables be marked as volatile?

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

      I'll need to think about that. To be honest, volatile is difficult to use correctly, but for 'signaling' type of applications it is likely useful.

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

    result = 42... (unintentional I guess haha)