std::condition_variable
Aus cppreference.com
<tbody>
</tbody>
| definiert in Header <condition_variable>
|
||
class condition_variable; |
(seit C++11) | |
Die condition_variable Klasse ist ein grundlegendes Synchronisierungsmittel das verwendet werden kann, um einen Thread oder mehrere Threads gleichzeitig zu blockieren, bis:
- eine Benachrichtigung von einem anderen Thread empfangen wird
- es zu einer Zeitüberschreitung kommt, oder
- ein spurious wakeup auftritt
Jeder Thread, der auf einer std::condition_variable warten möchte, muss zuerst ein std::unique_lock erwerben. Die wait-Operationen geben den gelockten Mutex atomar frei und pausieren den aktuellen Thread. Wenn die condition-Variable benachrichtigt wird, wird der Thread geweckt, und der Mutex wieder gelockt.
Die Klasse std::condition_variable ist ein StandardLayoutType. Sie ist nicht CopyConstructible, MoveConstructible, CopyAssignable, MoveAssignable.
Member-Typen
| Member-Typ | Definition |
native_handle_type
|
implementierungsabhängig |
Member-Funktionen
Baut das Objekt 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. (öffentliche Elementfunktion) | |
zerstört sich das Objekt 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. (öffentliche Elementfunktion) | |
operator= [gelöscht] |
nicht kopieren übertragbar 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. (öffentliche Elementfunktion) |
Benachrichtigen | |
| Informiert einen wartenden Thread (öffentliche Elementfunktion) | |
| Benachrichtigt alle wartenden Threads (öffentliche Elementfunktion) | |
Warten | |
Blockiert den aktuellen Thread, bis die condition variable geweckt wird 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. (öffentliche Elementfunktion) | |
Blockiert den aktuellen Thread, bis die condition variable geweckt wird oder die angegebenene Zeitspanne abgelaufen ist 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. (öffentliche Elementfunktion) | |
Blockiert den aktuellen Thread, bis die condition variable geweckt wird oder bis der angegebenene Zeitpunkt erreicht wurde 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. (öffentliche Elementfunktion) | |
Native Handle | |
gibt das systemeigene Handle zurück 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. (öffentliche Elementfunktion) | |
Beispiel
#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;
notified = 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