concept notify_one() in category c++

appears as: notify_one()
C++ Concurrency in Action, Second Edition

This is an excerpt from Manning's book C++ Concurrency in Action, Second Edition.

First off, you have a queue 1 that’s used to pass the data between the two threads. When the data is ready, the thread preparing the data locks the mutex protecting the queue using a std::lock_guard and pushes the data onto the queue 2. It then calls the notify_one() member function on the std::condition_variable instance to notify the waiting thread (if there is one) 3. Note that you put the code to push the data onto the queue in a smaller scope, so you notify the condition variable after unlocking the mutex — this is so that, if the waiting thread wakes immediately, it doesn’t then have to block again, waiting for you to unlock the mutex.

  • Synchronization Calls to notify_one(), notify_all(), wait(), wait_for(), and wait_until() on a single std::condition_variable instance are serialized. A call to notify_one() or notify_all() will only wake threads that started waiting prior to that call.
  • sitemap

    Unable to load book!

    The book could not be loaded.

    (try again in a couple of minutes)

    manning.com homepage
    test yourself with a liveTest