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()istrue, and the lock is owned by the calling thread.lk.mutex()shall return the same value as for any of the lock objects supplied towait(),wait_for()orwait_until()oncvfrom concurrently waiting threads.- Effects:
-
Transfers ownership of the lock held by
lkinto internal storage and schedulescvto be notified when the calling thread exits. This notification shall be as-iflk.unlock(); cv.notify_all();
- Throws:
std::system_errorif 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>