Skip to content

CP: std::condition_variable(), unlock mutex before notfifying waiting thread(s)? #1272

Closed

Description

@franzhollerer

Looking information on std::condition_variable I found the following example on https://en.cppreference.com/w/cpp/thread/condition_variable:

void worker_thread()
{
    :
    :
    // Manual unlocking is done before notifying, to avoid waking up
    // the waiting thread only to block again (see notify_one for details)
    lk.unlock();
    cv.notify_one();
}

And the following note regarding notify_one() / notify_all():

The notifying thread does not need to hold the lock on the same mutex as
the one held by the waiting thread(s); in fact doing so is a
pessimization, since the notified thread would immediately block again,
waiting for the notifying thread to release the lock. However, some
implementations (in particular many implementations of pthreads)
recognize this situation and avoid this "hurry up and wait" scenario by
transferring the waiting thread from the condition variable's queue
directly to the queue of the mutex within the notify call, without
waking it up.

Question 1: Is this a good advice?

Question 2: Would it make sense to address it in the C++ Core Guidelines too?

Activity

  1. jwakely commented on Sep 24, 2018

    @jwakely
    Contributor

    It's reasonable advice for performance, but that's not the main focus of the guidelines here. It's a minor optimisation, but not necessary for correctness.

  2. cubbimew commented on Sep 24, 2018

    @cubbimew
    Member

    Wasn't the plan to provide the opposite guideline in #554 (comment) ?

  3. jwakely commented on Sep 24, 2018

    @jwakely
    Contributor

    Yes, that's what Howard suggested, and he gave a (IMHO uncommon) example where it matters. And I objected to recommending it by default, as it's not part of the "how to use condvars correctly" advice I was proposing there.

    So"unlock first" is advice that doesn't usually make any difference for performance, and sometimes is unsafe (if the mutex lock is needed to ensure the CV remains alive). So not a good guideline.

  4. added a commit that references this issue on Jul 1, 2020
  5. Chukwujiobi-Canon commented on May 29, 2023

    @Chukwujiobi-Canon

    So"unlock first" is advice that doesn't usually make any difference for performance, and sometimes is unsafe (if the mutex lock is needed to ensure the CV remains alive).

    Please @jwakely sorry to bring this up again after over 5 years but can you elaborate further on this? An instance or an example will help a lot.

  6. jwakely commented on Jan 29, 2025

    @jwakely
    Contributor

    The cases where the notifying thread needs to lock the mutex are:

    • to ensure another thread doesn't destroy the condvar before the notify, e.g. because some other thread sees that the condition has become true and deletes some object which contains the condvar. Holding the mutex while notifying can be used to prevent other heads seeing that the condition is true until after the notify has been sent.
    • to ensure that another thread won't check the condition when it's false, but then it becomes true and the notify is done, then the first thread waits on the condvar, but it already missed the notification. This often happens when using an atomic variable for the condition and people think that the atomic variable avoids any data races. The updates to the atomic variable might be safe, but there's still a chance that its value can change after you check it and before you start to sleep. Holding the mutex while notifying (and when checking the condition and waiting in another thread) ensures that the notification isn't missed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions