名稱空間
變體
操作

std::condition_variable

來自 cppreference.com
< cpp‎ | thread
 
 
併發支援庫
執行緒
(C++11)
(C++20)
this_thread 名稱空間
(C++11)
(C++11)
(C++11)
協同取消
互斥
(C++11)
通用鎖管理
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
條件變數
condition_variable
(C++11)
(C++11)
訊號量
門閂和屏障
(C++20)
(C++20)
期值
(C++11)
(C++11)
(C++11)
(C++11)
安全回收
(C++26)
危險指標
原子型別
(C++11)
(C++20)
原子型別的初始化
(C++11)(C++20 中已棄用)
(C++11)(C++20 中已棄用)
記憶體排序
(C++11)(C++26 中已棄用)
原子操作的自由函式
原子標誌的自由函式
 
 
在標頭檔案 <condition_variable> 中定義
class condition_variable;
(C++11 起)

std::condition_variable 是一個同步原語,與 std::mutex 配合使用,用於阻塞一個或多個執行緒,直到另一個執行緒修改共享變數(即條件)並通知 std::condition_variable

打算修改共享變數的執行緒必須

  1. 獲取一個 std::mutex(通常透過 std::lock_guard)。
  2. 在持有鎖的同時修改共享變數。
  3. std::condition_variable 上呼叫 notify_onenotify_all(可以在釋放鎖後執行)。

即使共享變數是原子的,它也必須在持有互斥鎖的情況下修改,才能正確地將修改釋出給等待執行緒。

任何打算在 std::condition_variable 上等待的執行緒必須

  1. 獲取一個 std::unique_lock<std::mutex> 在用於保護共享變數的互斥鎖上。
  2. 執行以下操作之一
  1. 檢查條件,以防它已被更新並通知。
  2. std::condition_variable 上呼叫 waitwait_forwait_until(原子地釋放互斥鎖並暫停執行緒執行,直到條件變數被通知、超時過期或發生虛假喚醒,然後原子地獲取互斥鎖後再返回)。
  3. 檢查條件,如果未滿足則繼續等待。
  1. 使用 waitwait_forwait_until 的帶謂詞的過載,它執行相同的三個步驟。

std::condition_variable 僅與 std::unique_lock<std::mutex> 配合使用,這允許在某些平臺上實現最大效率。std::condition_variable_any 提供了一個條件變數,可以與任何 BasicLockable 物件配合使用,例如 std::shared_lock

條件變數允許併發呼叫 waitwait_forwait_untilnotify_onenotify_all 成員函式。

std::condition_variable 是一個 StandardLayoutType。它不可 CopyConstructible、不可 MoveConstructible、不可 CopyAssignable,也不可 MoveAssignable

目錄

[編輯] 巢狀型別

名稱 定義
native_handle_type 實現定義

[編輯] 成員函式

構造物件
(public member function) [編輯]
析構物件
(public member function) [編輯]
operator=
[已刪除]
不可複製賦值
(public member function) [編輯]
通知 (Notification)
通知一個等待執行緒
(public member function) [編輯]
通知所有等待執行緒
(public member function) [編輯]
等待 (Waiting)
阻塞當前執行緒直到條件變數被喚醒
(public member function) [編輯]
阻塞當前執行緒,直到條件變數被喚醒或達到指定的超時時長。
(public member function) [編輯]
阻塞當前執行緒,直到條件變數被喚醒或達到指定的時間點。
(public member function) [編輯]
原生控制代碼
返回原生控制代碼
(public member function) [編輯]

[編輯] 示例

std::condition_variablestd::mutex 結合使用,以促進執行緒間通訊。

#include <condition_variable>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
 
std::mutex m;
std::condition_variable cv;
std::string data;
bool ready = false;
bool processed = false;
 
void worker_thread()
{
    // wait until main() sends data
    std::unique_lock lk(m);
    cv.wait(lk, []{ return ready; });
 
    // after the wait, we own the lock
    std::cout << "Worker thread is processing data\n";
    data += " after processing";
 
    // send data back to main()
    processed = true;
    std::cout << "Worker thread signals data processing completed\n";
 
    // manual unlocking is done before notifying, to avoid waking up
    // the waiting thread only to block again (see notify_one for details)
    lk.unlock();
    cv.notify_one();
}
 
int main()
{
    std::thread worker(worker_thread);
 
    data = "Example data";
    // send data to the worker thread
    {
        std::lock_guard lk(m);
        ready = true;
        std::cout << "main() signals data ready for processing\n";
    }
    cv.notify_one();
 
    // wait for the worker
    {
        std::unique_lock lk(m);
        cv.wait(lk, []{ return processed; });
    }
    std::cout << "Back in main(), data = " << data << '\n';
 
    worker.join();
}

輸出

main() signals data ready for processing
Worker thread is processing data
Worker thread signals data processing completed
Back in main(), data = Example data after processing

[編輯] 另請參閱

提供與任意鎖型別關聯的條件變數
(class) [編輯]
(C++11)
提供基本的互斥設施
(class) [編輯]
實現嚴格基於作用域的互斥體所有權包裝器
(class template) [編輯]
實現可移動的互斥體所有權包裝器
(class template) [編輯]