Namensräume
Varianten
Aktionen

std::condition_variable

Aus cppreference.com
< cpp‎ | thread


 
 
Thema Support-Bibliothek
Threads
Original:
Threads
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
thread(C++11)
this_thread Namespace
Original:
this_thread namespace
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
get_id(C++11)
yield(C++11)
sleep_for(C++11)
sleep_until(C++11)
Gegenseitigen Ausschluss
Original:
Mutual exclusion
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
mutex(C++11)
timed_mutex(C++11)
Generische Sperrverwaltung
Original:
Generic lock management
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
lock_guard(C++11)
unique_lock(C++11)
defer_lock_t
try_to_lock_t
adopt_lock_t
(C++11)
(C++11)
(C++11)
lock(C++11)
try_lock(C++11)
defer_lock
try_to_lock
adopt_lock
(C++11)
(C++11)
(C++11)
Zustand Variablen
Original:
Condition variables
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
condition_variable(C++11)
condition_variable_any(C++11)
notify_all_at_thread_exit(C++11)
cv_status(C++11)
Futures
Original:
Futures
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
promise(C++11)
future(C++11)
shared_future(C++11)
packaged_task(C++11)
async(C++11)
launch(C++11)
future_status(C++11)
future_error(C++11)
future_category(C++11)
future_errc(C++11)
 
std::condition_variable
Member-Funktionen
Original:
Member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
condition_variable::condition_variable
condition_variable::~condition_variable
Notification
Original:
Notification
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
condition_variable::notify_one
condition_variable::notify_all
Warten
Original:
Waiting
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
condition_variable::wait
condition_variable::wait_for
condition_variable::wait_until
Ureinwohner Griff
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.
condition_variable::native handle
 
Defined in header <condition_variable>
class condition_variable;
(seit C++11)
Die condition_variable Klasse ist ein Synchronisierungsgrundeinheit das verwendet werden kann, um einen Faden oder mehrere Fäden gleichzeitig zu blockieren, bis:
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.
  • eine Benachrichtigung von einem anderen Thread empfangen
    Original:
    a notification is received from another thread
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • zu einer Zeitüberschreitung oder
    Original:
    a timeout expires, or
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • ein unechten wakeup auftritt
    Original:
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
Jeder Thread, der auf std::condition_variable warten möchte, muss sich eine std::unique_lock ersten erwerben. Die Wartevorgänge atomar den Mutex freigeben und setzt die Ausführung des Threads. Wenn die Bedingung variable benachrichtigt wird, wird der Thread geweckt, und der Mutex zurückerworben .
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.
Die Klasse std::condition_variable ist ein StandardLayoutType. Es ist nicht 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.

Inhaltsverzeichnis

[Bearbeiten] Mitglied Typen

Mitglied Typ
Original:
Member type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Definition
native_handle_type
Implementierung definiert
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.

[Bearbeiten] 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) [edit]
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) [edit]
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) [edit]
Notification
Original:
Notification
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
meldet ein wartender Thread
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.

(öffentliche Elementfunktion) [edit]
benachrichtigt alle wartenden Threads
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.

(öffentliche Elementfunktion) [edit]
Warten
Original:
Waiting
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Blockiert den aktuellen Thread, bis die Bedingung variable wird aufgeweckt
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) [edit]
blockiert den aktuellen Thread, bis die Bedingung Variable wird nach oben oder aufgewacht nach der angegebenen Zeitsperre Dauer
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) [edit]
Blockiert den aktuellen Thread, bis die Bedingung Variable wird nach oben oder geweckt bis angegebenen 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) [edit]
Ureinwohner Griff
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.
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) [edit]

[Bearbeiten] 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;
        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