std::condition_variable - cppreference.com
Da cppreference.com.
|
|
Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate. La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
<tbody> </tbody>
| Elemento definito nell'header <condition_variable> |
||
|
|
(dal C++11) | |
La classe condition_variable è una primitiva di sincronizzazione che può essere utilizzata per bloccare un thread, o più thread contemporaneamente, fino a che:
- viene ricevuta una notifica da un altro thread
- scade un timeout
- si verifica una wakeup spuria
Qualsiasi thread che intende aspettare std::condition_variable deve acquisire una std::unique_lock prima. Le operazioni di attesa atomicamente rilasciare il mutex e sospendere l'esecuzione del thread. Quando la variabile di condizione è notificato, il filo si risveglia, e il mutex viene riacquisito.
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.
La classe è un std::condition_variable StandardLayoutType. Non è CopyConstructible, MoveConstructible, CopyAssignable, MoveAssignable.
Original:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Membri tipi
Membro tipo Original: Member type The text has been machine-translated via Google Translate. |
Definition |
native_handle_type
|
Implementazione definita Original: implementation-defined The text has been machine-translated via Google Translate. |
Membri funzioni
Costruisce l'oggetto Original: constructs the object The text has been machine-translated via Google Translate. (metodo pubblico) [modifica] | |
distrugge l'oggetto Original: destructs the object The text has been machine-translated via Google Translate. (metodo pubblico) [modifica] | |
operator= [cancellato] |
non copia-assegnabile Original: not copy-assignable The text has been machine-translated via Google Translate. (metodo pubblico) [modifica] |
Notifica Original: Notification The text has been machine-translated via Google Translate. | |
segnala un thread in attesa Original: notifies one waiting thread The text has been machine-translated via Google Translate. (metodo pubblico) [modifica] | |
informa tutti i thread in attesa Original: notifies all waiting threads The text has been machine-translated via Google Translate. (metodo pubblico) [modifica] | |
In attesa Original: Waiting The text has been machine-translated via Google Translate. | |
blocca il thread corrente fino a quando la variabile di condizione è svegliato Original: blocks the current thread until the condition variable is woken up The text has been machine-translated via Google Translate. (metodo pubblico) [modifica] | |
Blocca il thread corrente fino a quando la variabile di condizione è svegliato o dopo la durata del timeout specificato 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. (metodo pubblico) [modifica] | |
blocca il thread corrente fino a quando la variabile di condizione è svegliato o fino a che punto nel tempo specificato è stato raggiunto 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. (metodo pubblico) [modifica] | |
Maniglia nativo Original: Native handle The text has been machine-translated via Google Translate. | |
restituisce l'handle nativo Original: returns the native handle The text has been machine-translated via Google Translate. (metodo pubblico) [modifica] | |
Esempio
#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(); }
Possible output:
producing 0 consuming 0 producing 1 consuming 1 producing 2 consuming 2 producing 3 consuming 3 producing 4 consuming 4