Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

12
  • 1
    Do you mean that wait() method unlocks mutex? Commented May 3, 2017 at 13:03
  • 1
    @AkasataAkasata wait simultaneously unlocks the mutex and enters a wait state on the condition variable. when the condition variable is notified, all waiters re-acquire the lock in order to test the condition. I'll annotate the code. Commented May 3, 2017 at 13:05
  • 1
    So if they are re-acquiring the lock in order, notify_all() wakes them in order too, right? Commented May 3, 2017 at 13:08
  • 1
    @AkasataAkasata I don't think the order of unlocking is guaranteed by the standard. Some docs here: en.cppreference.com/w/cpp/thread/condition_variable/wait Commented May 3, 2017 at 13:11
  • 3
    @RichardHodges, Only one thread can hold the lock at any given time, and it is not possible for cv.wait() to return until the thread holds the lock. So, yeah, it wakes them all simultaneously, but "wake" only means that the threads are moved from the condition variable's queue to the mutex queue. They won't all get the lock and return from wait() at the same time. Commented May 3, 2017 at 13:32