std::condition_variable

std::condition_variable

C++

编译器支持

独立和宿主

语言

标准库

标准库头文件

具名要求

特性测试宏 (C++20)

语言支持库

概念库 (C++20)

诊断库

内存管理库

元编程库 (C++11)

通用工具库

容器库

迭代器库

范围库 (C++20)

算法库

字符串库

文本处理库

数值库

日期和时间库

输入/输出库

文件系统库 (C++17)

并发支持库 (C++11)

执行控制库 (C++26)

技术规范

符号索引

外部库

[编辑] 并发支持库

线程

thread(C++11)

jthread(C++20)

hardware_destructive_interference_sizehardware_constructive_interference_size(C++17)(C++17)

this_thread 命名空间

get_id(C++11)

yield(C++11)

sleep_for(C++11)

sleep_until(C++11)

协作式取消

stop_token(C++20)

inplace_stop_token(C++26)

never_stop_token(C++26)

stop_source(C++20)

inplace_stop_source(C++26)

stop_callback(C++20)

inplace_stop_callback(C++26)

stop_callback_for_t(C++26)

stoppable_token(C++26)

unstoppable_token(C++26)

stoppable-source(C++26)

stoppable-callback-for(C++26)

互斥

mutex(C++11)

recursive_mutex(C++11)

shared_mutex(C++17)

timed_mutex(C++11)

recursive_timed_mutex(C++11)

shared_timed_mutex(C++14)

通用锁管理

lock(C++11)

lock_guard(C++11)

scoped_lock(C++17)

unique_lock(C++11)

shared_lock(C++14)

once_flag(C++11)

call_once(C++11)

try_lock(C++11)

defer_locktry_to_lockadopt_lockdefer_lock_ttry_to_lock_tadopt_lock_t(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)

条件变量

condition_variable(C++11)

condition_variable_any(C++11)

notify_all_at_thread_exit(C++11)

cv_status(C++11)

信号量

counting_semaphorebinary_semaphore(C++20)(C++20)

闩锁和屏障

latch(C++20)

barrier(C++20)

期值

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)

安全回收

rcu_obj_base(C++26)

rcu_domain(C++26)

rcu_default_domain(C++26)

rcu_synchronize(C++26)

rcu_barrier(C++26)

rcu_retire(C++26)

危险指针

hazard_pointer_obj_base(C++26)

hazard_pointer(C++26)

make_hazard_pointer(C++26)

原子类型

atomic(C++11)

atomic_ref(C++20)

atomic_flag(C++11)

原子类型的初始化

atomic_init(C++11)(在 C++20 中已弃用)

ATOMIC_VAR_INIT(C++11)(在 C++20 中已弃用)

ATOMIC_FLAG_INIT(C++11)

内存顺序

memory_order(C++11)

kill_dependency(C++11)(在 C++26 中已弃用)

atomic_thread_fence(C++11)

atomic_signal_fence(C++11)

原子操作的自由函数

atomic_storeatomic_store_explicit(C++11)(C++11)

atomic_loadatomic_load_explicit(C++11)(C++11)

atomic_exchangeatomic_exchange_explicit(C++11)(C++11)

atomic_compare_exchange_weakatomic_compare_exchange_weak_explicitatomic_compare_exchange_strongatomic_compare_exchange_strong_explicit(C++11)(C++11)(C++11)(C++11)

atomic_fetch_addatomic_fetch_add_explicit(C++11)(C++11)

atomic_fetch_subatomic_fetch_sub_explicit(C++11)(C++11)

atomic_fetch_andatomic_fetch_and_explicit(C++11)(C++11)

atomic_fetch_oratomic_fetch_or_explicit(C++11)(C++11)

atomic_fetch_xoratomic_fetch_xor_explicit(C++11)(C++11)

atomic_fetch_maxatomic_fetch_max_explicit(C++26)(C++26)

atomic_fetch_minatomic_fetch_min_explicit(C++26)(C++26)

atomic_is_lock_free(C++11)

atomic_waitatomic_wait_explicit(C++20)(C++20)

atomic_notify_one(C++20)

atomic_notify_all(C++20)

原子标志的自由函数

atomic_flag_test_and_setatomic_flag_test_and_set_explicit(C++11)(C++11)

atomic_flag_clearatomic_flag_clear_explicit(C++11)(C++11)

atomic_flag_testatomic_flag_test_explicit(C++20)(C++20)

atomic_flag_waitatomic_flag_wait_explicit(C++20)(C++20)

atomic_flag_notify_one(C++20)

atomic_flag_notify_all(C++20)

[编辑] std::condition_variable

成员函数

condition_variable::condition_variable

condition_variable::~condition_variable

通知

condition_variable::notify_one

condition_variable::notify_all

等待

condition_variable::wait

condition_variable::wait_for

condition_variable::wait_until

原生句柄

condition_variable::native handle

[编辑]

定义于头文件

class condition_variable;

(since C++11)

std::condition_variable 是一个同步原语,与 std::mutex 一起使用,以阻塞一个或多个线程,直到另一个线程修改共享变量(条件)并通知 std::condition_variable。

打算修改共享变量的线程必须

获取 std::mutex (通常通过 std::lock_guard)。在拥有锁时修改共享变量。在 std::condition_variable 上调用 notify_one 或 notify_all (可以在释放锁之后完成)。

即使共享变量是原子的,也必须在拥有互斥锁时修改它,才能正确地将修改发布到等待线程。

任何打算在 std::condition_variable 上等待的线程必须

在用于保护共享变量的互斥锁上获取 std::unique_lock。执行以下操作之一

检查条件,以防它已经被更新和通知。在 std::condition_variable 上调用 wait、wait_for 或 wait_until (原子地释放互斥锁并暂停线程执行,直到条件变量被通知、超时到期或发生伪唤醒,然后在返回之前原子地获取互斥锁)。检查条件,如果未满足则继续等待。

使用 wait、wait_for 和 wait_until 的谓词重载,它执行相同的三个步骤。

std::condition_variable 仅与 std::unique_lock 一起工作,这允许在某些平台上实现最大效率。std::condition_variable_any 提供了一个条件变量,它可以与任何 BasicLockable 对象一起工作,例如 std::shared_lock。

条件变量允许并发调用 wait、wait_for、wait_until、notify_one 和 notify_all 成员函数。

类 std::condition_variable 是一个 StandardLayoutType。它不是 CopyConstructible、MoveConstructible、CopyAssignable 或 MoveAssignable。

目录

1 嵌套类型

2 成员函数

2.1 通知

2.2 等待

2.3 原生句柄

3 示例

4 参见

[edit] 嵌套类型

名称

定义

native_handle_type

实现定义

[edit] 成员函数

(构造函数)

构造对象 (公共成员函数) [edit]

(析构函数)

析构对象 (公共成员函数) [edit]

operator=[已删除]

不可复制赋值 (公共成员函数) [edit]

通知

notify_one

通知一个等待线程 (公共成员函数) [edit]

notify_all

通知所有等待线程 (公共成员函数) [edit]

等待

wait

阻塞当前线程,直到条件变量被唤醒 (公共成员函数) [edit]

wait_for

阻塞当前线程,直到条件变量被唤醒或超过指定的超时时长 (公共成员函数) [edit]

wait_until

阻塞当前线程,直到条件变量被唤醒或到达指定时间点 (公共成员函数) [edit]

原生句柄

native_handle

返回原生句柄 (公共成员函数) [edit]

[edit] 示例

std::condition_variable 与 std::mutex 结合使用,以促进线程间通信。

运行此代码

#include

#include

#include

#include

#include

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

[edit] 参见

condition_variable_any(C++11)

提供与任何锁类型关联的条件变量 (类) [edit]

mutex(C++11)

提供基本互斥设施 (类) [edit]

lock_guard(C++11)

实现严格基于作用域的互斥锁所有权包装器 (类模板) [edit]

unique_lock(C++11)

实现可移动的互斥锁所有权包装器 (类模板) [edit]

相关推荐

路由宝十大品牌排行榜
365bet日博

路由宝十大品牌排行榜

📅 07-12 👁️ 7113
逆水寒九灵全方位职业攻略 九灵怎么打
365bet官网投注

逆水寒九灵全方位职业攻略 九灵怎么打

📅 07-09 👁️ 6860
彭禺厶《鬼怪奇谈祥云寺》.mp4
365bet官网投注

彭禺厶《鬼怪奇谈祥云寺》.mp4

📅 07-20 👁️ 6088