std::condition_variable 和 std::condition_variable_any 有什么区别? [英] What is the difference between std::condition_variable and std::condition_variable_any?
问题描述
我可能遗漏了一些明显的东西,但我看不出 std::condition_variable
和 std::condition_variable_any
之间有什么区别.为什么我们需要两者?
I'm probably missing something obvious, but I can't see any difference between std::condition_variable
and std::condition_variable_any
. Why do we need both?
推荐答案
std::condition_variable
更专业,因此在不需要 的灵活性时可以更高效std::condition_variable_any
.
std::condition_variable
is more specialized, and therefore can be more efficient when you don't need the flexibility of std::condition_variable_any
.
来自 N3290 §30.5[thread.condition]/1
From N3290 §30.5[thread.condition]/1
类 condition_variable
提供一个只能等待 unique_lock
类型的对象的条件变量,允许最大在某些平台上的效率.condition_variable_any
类提供了一个通用条件变量可以等待用户提供的锁类型的对象.
Class
condition_variable
provides a condition variable that can only wait on an object of typeunique_lock<mutex>
, allowing maximum efficiency on some platforms. Classcondition_variable_any
provides a general condition variable that can wait on objects of user-supplied lock types.
实际上,在 LLVM 的 libc++ 中,condition_variable_any
是在 shared_mutex 上使用更专业的 condition_variable
(使用 pthread_cond_t)实现的.
Actually, in LLVM's libc++, condition_variable_any
is implemented using the more specialized condition_variable
(which uses pthread_cond_t) on a shared_mutex.
这篇关于std::condition_variable 和 std::condition_variable_any 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!