std::notify_all_at_thread_exit
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <condition_variable> 中定义
|
||
void notify_all_at_thread_exit( std::condition_variable& cond, std::unique_lock<std::mutex> lk ); |
(C++11 起) | |
。
notify_all_at_thread_exit
提供了一个机制来通知其他线程,一个给定的线程已经完全结束,包括销毁所有。 thread_local。对象。它的工作如下:原文:
notify_all_at_thread_exit
provides a mechanism to notify other threads that a given thread has completely finished, including destroying all 。 thread_local。 objects. It operates as follows:- 。先前获得的锁
lk
的所有权转移到内部存储.原文:Ownership of the previously acquired locklk
is transferred to internal storage.
- 。的执行环境中被修改,例如,当前线程退出时,与。线程局部存储的时间。的所有对象的析构函数被称为后,被通知的条件变量
cond
犹如:。原文:The execution environment is modified such that when the current thread exits, after the destructors for all objects with 。线程局部存储的时间。 are called, the condition variablecond
is notified as if by:
lk.unlock();
cond.notify_all();
。同样的效果可以达到与提供的设施std::promise或std::packaged_task.
原文:
An equivalent effect may be achieved with the facilities provided by std::promise or std::packaged_task.
目录 |
[编辑] 。注释。
。
lock.mutex()
调用此函数,如果没有被锁定由当前线程是不确定的行为.原文:
Calling this function if
lock.mutex()
is not locked by the current thread is undefined behavior.。
lock.mutex()
调用此函数,如果使用的所有其他线程当前正在等待同一个条件变量是不确定的行为是不相同的互斥.原文:
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.。所提供的锁
lk
持有,直到该线程退出。一旦这个函数被调用,没有多个线程可能获取同一个锁,以等待cond
。如果某个线程正在等待该条件变量,它不应该试图释放和重新获取锁,当它醒来不合逻辑.原文:
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.
[编辑] 。参数。
cond | - | 。条件变量的线程退出通知。
原文: the condition variable to notify at thread exit |
lk | - | 。相关联的锁的条件变量
cond 。 原文: the lock associated with the condition variable cond |
===。 返回值。===
。 (无)。
[编辑] 。为例。
。
notify_all_at_thread_exit
这部分的代码段说明了如何可以用来避免访问数据依赖于线程当地人的当地人都在这个过程中被破坏,而这些线程。
原文:
This partial code fragment illustrates how
notify_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:
#include <mutex> #include <thread> std::mutex m; std::condition_variable cv; bool ready = false; ComplexType result; // some arbitrary type void thread_func() { std::unique_lock<std::mutex> lk(m); // assign a value to result using thread_local data result = function_that_uses_thread_locals(); 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 thread std::unique_lock<std::mutex> lk(m); while(!ready) { cv.wait(lk); } process(result); // result is ready and thread_local destructors have finished }
[编辑] 。另请参阅。
设置特定值的结果,同时提供了只有在线程退出的通知 原文: sets the result to specific value while delivering the notification only at thread exit (公共成员函数of std::promise )
| |
执行的结果是只有一次准备退出当前线程的功能 原文: executes the function ensuring that the result is ready only once the current thread exits (公共成员函数of std::packaged_task )
|