Namespaces
Variants
Views
Actions

std::condition_variable

From cppreference.com
< cpp‎ | thread
Revision as of 18:42, 2 April 2013 by Cubbi (Talk | contribs)

Defined in header <condition_variable>
class condition_variable;
(since C++11)

The condition_variable class is a synchronization primitive that can be used to block a thread, or multiple threads at the same time, until:

  • a notification is received from another thread
  • a timeout expires, or
  • a spurious wakeup occurs

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 class std::condition_variable is a StandardLayoutType. It is not CopyConstructible, MoveConstructible, CopyAssignable, MoveAssignable.

Contents

Member types

Member type Definition
native_handle_type implementation-defined

Member functions

Template:cpp/thread/condition variable/dcl list constructorTemplate:cpp/thread/condition variable/dcl list destructorTemplate:cpp/thread/condition variable/dcl list operator=Template:cpp/thread/condition variable/dcl list notify oneTemplate:cpp/thread/condition variable/dcl list notify allTemplate:cpp/thread/condition variable/dcl list waitTemplate:cpp/thread/condition variable/dcl list wait forTemplate:cpp/thread/condition variable/dcl list wait untilTemplate:cpp/thread/condition variable/dcl list native handle
Notification
Waiting
Native handle

Example

#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::lock_guard<std::mutex> lock(m);
            std::cout << "producing " << i << '\n';
            produced_nums.push(i);
            notified = true;
            cond_var.notify_one();
        }   
 
        std::lock_guard<std::mutex> lock(m);  
        notified = true;
        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