Namespaces
Variants
Views
Actions

std::notify_all_at_thread_exit

From cppreference.com
 
 
Thread support library
Threads
thread(C++11)
this_thread namespace
get_id(C++11)
yield(C++11)
sleep_for(C++11)
sleep_until(C++11)
Mutual exclusion
mutex(C++11)
timed_mutex(C++11)
Generic lock management
lock_guard(C++11)
unique_lock(C++11)
defer_lock_t
try_to_lock_t
adopt_lock_t
(C++11)
(C++11)
(C++11)
lock(C++11)
try_lock(C++11)
defer_lock
try_to_lock
adopt_lock
(C++11)
(C++11)
(C++11)
Condition variables
condition_variable(C++11)
condition_variable_any(C++11)
notify_all_at_thread_exit(C++11)
cv_status(C++11)
Futures
promise(C++11)
future(C++11)
shared_future(C++11)
packaged_task(C++11)
async(C++11)
launch(C++11)
future_status(C++11)
future_error(C++11)
future_category(C++11)
future_errc(C++11)
 
Defined in header <condition_variable>
void notify_all_at_thread_exit( std::condition_variable& cond,
                                std::unique_lock<std::mutex> lk );
(since C++11)

Transfers the ownership of the supplied, previously acquired, lock lk to internal storage and sets up the execution environment so when the current thread exits, after the destructors for all objects with thread-local storage duration are called, the condition variable is notified as in by

lk.unlock();
cond.notify_all();

The purpose of this function is to provide a mechanism to determine when all thread_local objects in a detached thread have been destructed. Equivalent effect may be achieved with the facilities provided by std::promise or std::packaged_task.

Contents

[edit] Notes

Calling this function if lock.mutex() is not locked by the current thread is undefined behavior.

Calling this function if lock.mutex() is not the same mutex as the one used by all other threads that are currently waiting on the same condition variable is undefined behavior.

The supplied lock lk is held until the thread exits. Once this function has been called, no more threads may acquire the same lock in order to wait on cond. If some thread is waiting on this condition variable, it should not attempt to release and reacquire the lock when it wakes up spuriously.

In typical use cases, this function is the last thing called by a detached thread.

[edit] Parameters

cond - the condition variable to notify at thread exit
lk - the lock associated with the condition variable cond

[edit] Return value

(none)

[edit] Example

Code fragment illustrating the intended use for notify_all_at_thread_exit

#include <mutex>
#include <thread>
 
std::mutex m;
std::condition_variable cv;
 
bool ready = false;
ComplexType result;
 
void thread_func()
{
    std::unique_lock<std::mutex> lk(m);
    result = function_that_uses_thread_locals();
    ready = true;
    std::notify_all_at_thread_exit(cv, std::move(lk));
} // destroy thread_locals, notify cv, unlock mutex
 
int main()
{
    std::thread t(thread_func);
    t.detach();
    // do other work
    std::unique_lock<std::mutex> lk(m);
    while(!ready) {
        cv.wait(lk); // wait for the detached thread
    }
    process(result); // result is ready and thread_locals are destructed
}

[edit] See also

sets the result to specific value while delivering the notification only at thread exit
(public member function of std::promise) [edit]
executes the function ensuring that the result is ready
only once the current thread exits
(public member function of std::packaged_task) [edit]