Espaços nominais
Variantes
Acções

std::condition_variable

Da cppreference.com
< cpp‎ | thread


 
 
Biblioteca de suporte a discussão
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.
(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.
(C++11)
(C++11)
(C++11)
Exclusão mútua
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.
(C++11)
Gestão de bloqueio genérico
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.
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
Variáveis ​​de condição
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)
(C++11)
Futuros
Original:
Futures
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(C++11)
(C++11)
(C++11)
(C++11)
 
std::condition_variable
Funções de membro
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
Notificação
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
Espera
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
Identificador nativo
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
 
Definido no cabeçalho <condition_variable>
class condition_variable;
(desde C++11)
A classe condition_variable é um primitivo de sincronização que pode ser utilizado para bloquear um segmento, ou vários segmentos, ao mesmo tempo, até 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.
  • uma notificação for recebida de outro segmento
    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.
  • um tempo limite expirar, ou
    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.
  • um wakeup espúria ocorre
    Original:
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
Qualquer segmento que pretende esperar std::condition_variable tem de adquirir uma std::unique_lock primeiro. As operações de espera atomicamente liberar o mutex e suspender a execução da rosca. Quando a variável de condição é notificado, o fio é despertada, e o mutex é readquirida.
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.
O std::condition_variable classe é um StandardLayoutType. Não é 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.

Índice

[editar] Tipos de membro

Tipo de membro
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
Definida pela implementação
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.

[editar] Funções de membro

Constrói o objeto
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.

(função pública membro) [edit]
destrói o objeto
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.

(função pública membro) [edit]
operator=
[excluída]
cópia não-transmissíveis
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.

(função pública membro) [edit]
Notificação
Original:
Notification
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
notifica um segmento de espera
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.

(função pública membro) [edit]
notifica todos os segmentos de espera
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.

(função pública membro) [edit]
Espera
Original:
Waiting
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
bloqueia o segmento atual até que a variável de condição é acordado
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.

(função pública membro) [edit]
Bloqueia o segmento atual até que a variável de condição é acordado ou após o período de tempo limite especificado
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.

(função pública membro) [edit]
bloqueia o segmento atual até que a variável de condição é acordado ou até ponto de tempo especificado foi alcançado
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.

(função pública membro) [edit]
Identificador nativo
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.
retorna o identificador nativo
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.

(função pública membro) [edit]

[editar] Exemplo

#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();
}

Potencial saída:

producing 0
consuming 0
producing 1
consuming 1
producing 2
consuming 2
producing 3
consuming 3
producing 4
consuming 4