Documentation Home >> Headers >> <condition_variable> Header >> std::condition_variable >> std::notify_all_at_thread_exit Non-member function

Wake all of the threads waiting on a std::condition_variable when the current thread exits.

void notify_all_at_thread_exit(
    condition_variable& cv,unique_lock<mutex> lk);

Preconditions:

lk.owns_lock() is true, and the lock is owned by the calling thread. lk.mutex() shall return the same value as for any of the lock objects supplied to wait(), wait_for() or wait_until() on cv from concurrently waiting threads.

Effects:

Transfers ownership of the lock held by lk into internal storage and schedules cv to be notified when the calling thread exits. This notification shall be as-if

lk.unlock();
cv.notify_all();
Throws:

std::system_error if the effects cannot be achieved.

Note:

The lock is held until the thread exits, so care must be taken to avoid deadlock. It is recommended that the calling thread should exit as soon as possible, and that no blocking operations are performed on this thread.

The user should ensure that waiting threads do not erroneously assume that the thread has exited when they are woken, particularly with the potential for spurious wake-ups. This can be achieved by testing a predicate on the waiting thread that is only made true by the notifying thread under the protection of the mutex, and without releasing the lock on the mutex prior to the call of notify_all_at_thread_exit

Header

#include <condition_variable>

See Also