std::notify_all_at_thread_exit
De cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Defined in header <condition_variable>
|
||
| void notify_all_at_thread_exit( std::condition_variable& cond, std::unique_lock<std::mutex> lk ); |
(ya que C + +11) | |
notify_all_at_thread_exit proporciona un mecanismo para notificar a otros subprocesos que un subproceso dado se ha terminado completamente, incluyendo la destrucción de todos los objetos thread_local. Funciona de la siguiente manera:Original:
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:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- La propiedad de la
lkbloqueo previamente adquirida se transfiere a la memoria interna .Original:Ownership of the previously acquired locklkis transferred to internal storage.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- El entorno de ejecución es modificado de tal manera que cuando el hilo termina actuales, después de los destructores de todos los objetos con hilo duración de almacenamiento local son llamados, la
condvariable de condición se notifica como si por:Original:The execution environment is modified such that when the current thread exits, after the destructors for all objects with hilo duración de almacenamiento local are called, the condition variablecondis notified as if by:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
lk.unlock();
cond.notify_all();
Un efecto equivalente se puede lograr con los recursos proporcionados por std::promise o std::packaged_task .
Original:
An equivalent effect may be achieved with the facilities provided by std::promise or std::packaged_task.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Contenido |
[editar] Notas
Llamar a esta función si
lock.mutex() no está bloqueado por el flujo actual es un comportamiento indefinido .Original:
Calling this function if
lock.mutex() is not locked by the current thread is undefined behavior.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Llamar a esta función si
lock.mutex() no es el mutex mismo que el utilizado por todos los otros subprocesos que están a la espera de la variable misma condición es un comportamiento no definido .Original:
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 text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
El
lk bloqueo se mantiene hasta que suministra el hilo termina. Una vez que esta función se ha llamado, no hay más subprocesos pueden adquirir el mismo bloqueo con el fin de esperar en cond. Si un hilo está esperando en esta variable de condición, no debe tratar de liberar y volver a adquirir el bloqueo cuando se despierta falsamente .Original:
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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
En los casos de uso típicos, esta función es la última cosa que se llama de un hilo separado .
Original:
In typical use cases, this function is the last thing called by a detached thread.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[editar] Parámetros
| cond | - | la variable condición de notificar a la salida de hilo
Original: the condition variable to notify at thread exit The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| lk | - | el bloqueo asociado con el
cond variable de condición Original: the lock associated with the condition variable cond The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[editar] Valor de retorno
(Ninguno)
Original:
(none)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[editar] Ejemplo
Este fragmento parcial de código ilustra cómo
notify_all_at_thread_exit se puede utilizar para evitar acceder a datos que depende de los locales de rosca mientras que los locales de subprocesos están en proceso de ser destruido:
Original:
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:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#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 }
[editar] Ver también
| establece el resultado al valor específico, mientras que la entrega de la notificación, sólo en la salida de hilo Original: sets the result to specific value while delivering the notification only at thread exit The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (miembro público of std::promise función)
| |
| ejecuta la función de asegurar que el resultado está lista sólo una vez se cierra el subproceso actuales Original: executes the function ensuring that the result is ready only once the current thread exits The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (miembro público of std::packaged_task función)
| |