std::condition_variable
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. |
Déclaré dans l'en-tête <condition_variable> | ||
class condition_variable; | (depuis C++11) | |
La classe
condition_variable
est une primitive de synchronisation qui peut être utilisée pour bloquer un ou plusieurs threads en même temps, jusqu'à ce que:Original:
The
condition_variable
class is a synchronization primitive that can be used to block a thread, or multiple threads at the same time, until: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.
- une notification soit reçue d'un autre threadOriginal:a notification is received from another threadThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - un délai expire, ouOriginal:a timeout expires, orThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - un faux réveil se produiseOriginal:a réveil faux occursThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tout thread qui veut attendre une
La classe std::condition_variable
doit d'abord acquérir un std::unique_lock. Les opérations d'attente (wait) libèrent atomiquement le mutex et suspendent l'exécution du thread appelant. Lorsque la notification de la condition_variable (notify) intervient, le thread en attente est réveillé, et le mutex lui est acquis de nouveau.Original:
Any thread that intends to wait on
std::condition_variable
has to acquire a std::unique_lock first. The wait operations atomically release the mutex and suspend the execution of the thread. When the condition variable is notified, the thread is awakened, and the mutex is reacquired.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.
std::condition_variable
est de type StandardLayoutType
. Elle n'est pas CopyConstructible
, MoveConstructible
, CopyAssignable
, MoveAssignable
.Original:
The class
std::condition_variable
is a StandardLayoutType
. It is not CopyConstructible
, MoveConstructible
, CopyAssignable
, MoveAssignable
.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.
[modifier]Types de membres
Type du membre | Définition |
native_handle_type | ' Définie par l'implémentation Original: implementation-defined The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[modifier]Fonctions membres
Construit l'objet Original: constructs the object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction membre publique) | |
Détruit l'objet Original: destructs the object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction membre publique) | |
operator= [ supprimée ]Original: deleted The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | pas de copier-cessible Original: not copy-assignable The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction membre publique) |
Original: Notification The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
Notifie un thread en attente Original: notifies one waiting thread The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction membre publique) | |
Notifie tous les threads en attente Original: notifies all waiting threads The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction membre publique) | |
Original: Waiting The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
Bloque le thread actuel jusqu'à ce que la condition_variable soit réveillée Original: blocks the current thread until the condition variable is woken up The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction membre publique) | |
Bloque le thread actuel jusqu'à ce que la condition_variable soit réveillée ou que la durée de temporisation spécifiée soit écoulée Original: blocks the current thread until the condition variable is woken up or after the specified timeout duration The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction membre publique) | |
Bloque le thread en cours jusqu'à ce que la condition_variable soit réveillée ou jusqu'à ce que le point temporel (time point) spécifié soit atteint Original: blocks the current thread until the condition variable is woken up or until specified time point has been reached The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction membre publique) | |
Original: Native handle The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
Retourne le handle natif sous-jacent Original: returns the native handle The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction membre publique) |
[modifier]Exemple
#include <condition_variable>#include <mutex>#include <thread>#include <iostream>#include <queue>#include <chrono> int main(){std::queue<int> produced_nums;std::mutex m; std::condition_variable cond_var;bool done =false;bool notified =false; std::thread producer([&](){for(int i =0; i <5;++i){std::this_thread::sleep_for(std::chrono::seconds(1));std::unique_lock<std::mutex> lock(m);std::cout<<"producing "<< i <<'\n'; produced_nums.push(i); notified =true; cond_var.notify_one();} done =true; cond_var.notify_one();}); std::thread consumer([&](){std::unique_lock<std::mutex> lock(m);while(!done){while(!notified){// loop to avoid spurious wakeups cond_var.wait(lock);}while(!produced_nums.empty()){std::cout<<"consuming "<< produced_nums.front()<<'\n'; produced_nums.pop();} notified =false;}}); producer.join(); consumer.join();}
Résultat possible :
producing 0 consuming 0 producing 1 consuming 1 producing 2 consuming 2 producing 3 consuming 3 producing 4 consuming 4