Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::notify_all_at_thread_exit

      From cppreference.com
      <cpp‎ |thread
       
       
      Concurrency support library
      Threads
      (C++11)
      (C++20)
      this_thread namespace
      (C++11)
      (C++11)
      (C++11)
      Cooperative cancellation
      Mutual exclusion
      Generic lock management
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      Condition variables
      notify_all_at_thread_exit
      (C++11)
      (C++11)
      Semaphores
      Latches and Barriers
      (C++20)
      (C++20)
      Futures
      (C++11)
      (C++11)
      (C++11)
      Safe reclamation
      Hazard pointers
      Atomic types
      (C++11)
      (C++20)
      Initialization of atomic types
      (C++11)(deprecated in C++20)
      (C++11)(deprecated in C++20)
      Memory ordering
      (C++11)(deprecated in C++26)
      Free functions for atomic operations
      Free functions for atomic flags
       
      Defined in header<condition_variable>
      void notify_all_at_thread_exit(std::condition_variable& cond,
                                     std::unique_lock<std::mutex> lk);
      (since C++11)

      notify_all_at_thread_exit provides a mechanism to notify other threads that a given thread has completely finished, including destroying allthread_local objects. It operates as follows:

      • Ownership of the previously acquired locklk is transferred to internal storage.
      • The execution environment is modified such that when the current thread exits, the condition variablecond is notified as if bylk.unlock();
        cond.notify_all();
        .

      The impliedlk.unlock() issequenced after the destruction of all objects withthread local storage duration associated with the current thread.

      If any of the following conditions is satisfied, the behavior is undefined:

      • lk is not locked by the calling thread.
      • If some other threads are also waiting oncond,lk.mutex() is different from the mutex unlocked by the waiting functions (wait,wait_for andwait_until) called oncond by those threads.

      Contents

      [edit]Notes

      An equivalent effect may be achieved with the facilities provided bystd::promise orstd::packaged_task.

      The supplied locklk is held until the thread exits. Once this function has been called, no more threads may acquire the same lock in order to wait oncond. If some threads are waiting on this condition variable, ensure that the condition being waited for is satisfied while holding the lock onlk, and that this lock is not released and reacquired prior to callingnotify_all_at_thread_exit to avoid confusion from spurious wakeups in other threads.

      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 variablecond

      [edit]Return value

      (none)

      [edit]Example

      This partial code fragment illustrates hownotify_all_at_thread_exit can be used to avoid accessing data that depends on thread locals while those thread locals are in the process of being destructed:

      Run this code
      #include <cassert>#include <condition_variable>#include <mutex>#include <string>#include <thread> std::mutex m;std::condition_variable cv; bool ready=false;std::string result;// some arbitrary type void thread_func(){    thread_localstd::string thread_local_data="42"; std::unique_lock<std::mutex> lk(m); // assign a value to result using thread_local data    result= thread_local_data;    ready=true;     std::notify_all_at_thread_exit(cv, std::move(lk)); }// 1. destroy thread_locals;// 2. unlock mutex;// 3. notify cv. int main(){std::thread t(thread_func);    t.detach(); // do other work// ... // wait for the detached threadstd::unique_lock<std::mutex> lk(m);    cv.wait(lk,[]{return ready;}); // result is ready and thread_local destructors have finished, no UBassert(result=="42");}

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2140C++11the call tonotify_all_at_thread_exit
      synchronized with calls to functions waiting oncond
      updated the synchronization
      requirement

      [edit]See also

      sets the result to specific value while delivering the notification only at thread exit
      (public member function ofstd::promise<R>)[edit]
      executes the function ensuring that the result is ready only once the current thread exits
      (public member function ofstd::packaged_task<R(Args...)>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/thread/notify_all_at_thread_exit&oldid=170165"

      [8]ページ先頭

      ©2009-2025 Movatter.jp