[英]`std::condition_variable::wait_for` calls the predicate very often
请考虑以下codesnippet:
#include <iostream>
#include <condition_variable>
#include <chrono>
#include <mutex>
int main () {
std::mutex y;
std::condition_variable x;
std::unique_lock<std::mutex>lock{y};
int i = 0;
auto increment = [&] {++i; return false;};
using namespace std::chrono_literals;
//lock 5s if increment returns false
//let's see how often was increment called?
x.wait_for(lock, 5s, increment);
std::cout << i << std::endl;
//compare this with a simple loop:
//how often can my system call increment in 5s?
auto const end = std::chrono::system_clock::now() + 5s;
i = 0;
while (std::chrono::system_clock::now() < end) {
increment();
}
std::cout << i;
}
据我所知wait_for, i
应该在wait_for
之后是O(1)(让我们假设虚假解锁是罕见的)。
但是,我明白了
对于kernel 4.17.14, Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
, i ~= 3e8
kernel 4.17.14, Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
,
对于kernel 3.10.0, Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz
i ~= 8e6
。
这听起来很有趣,所以我通过比较一个运行5秒的简单循环来检查。 i
结果大致相同,只有5-10%的差异。
题:
wait_for
在做什么? 它是否按预期工作,我只是理解cppreference错误,或者我搞砸了?
第二,(可选)问题: i
这个巨大差异来自哪里?
附加信息:( gcc7.3
, gcc8.2
, clang6.0
),flags: -O3 --std=c++17
都可以得到可比较的结果。
你需要在编译时向gcc添加-pthread
标志,例如在RE上的gcc 5.1.0上:
without pthread: 49752692
with pthread: 2
您需要使用-pthread
标志链接到pthread库到g ++:
g++ cond_test.cpp -pthread
大多数Linux系统都要求您链接到pthread
库以使用线程功能。 但是,使用标准C ++线程的程序似乎在没有显式链接到pthread
情况下成功链接,而是在运行时产生未定义的行为(它经常崩溃,但使用此代码似乎不会,但会产生意外行为)。
此代码的示例:
$ g++ t.cpp && ./a.out
5817437
18860410
$ g++ t.cpp -pthread && ./a.out
2
19718764
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.