[英]`std::condition_variable::wait_for` calls the predicate very often
Consider the following codesnippet: 请考虑以下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;
}
As I understand wait_for, i
should be O(1) after wait_for
(let's assume spurious unlocks are rare). 据我所知wait_for,
i
应该在wait_for
之后是O(1)(让我们假设虚假解锁是罕见的)。
However, I get 但是,我明白了
i ~= 3e8
for kernel 4.17.14, Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
, 对于
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
,
i ~= 8e6
for kernel 3.10.0, Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz
. 对于
kernel 3.10.0, Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz
i ~= 8e6
。
This sounds funny, so i check by comparing with a simple loop that runs 5 seconds. 这听起来很有趣,所以我通过比较一个运行5秒的简单循环来检查。 Roughly same results for
i
, only a 5-10% difference. i
结果大致相同,只有5-10%的差异。
Question: 题:
what is wait_for
doing? wait_for
在做什么? Does it work as expected and I just understood cppreference wrong, or did I mess up? 它是否按预期工作,我只是理解cppreference错误,或者我搞砸了?
Second, (optional) question: Where does this enormous difference in i
come from? 第二,(可选)问题:
i
这个巨大差异来自哪里?
Additional info: ( gcc7.3
, gcc8.2
, clang6.0
), flags: -O3 --std=c++17
all yield comparable results. 附加信息:(
gcc7.3
, gcc8.2
, clang6.0
),flags: -O3 --std=c++17
都可以得到可比较的结果。
libstdc++ has an unfortunate ability to compile and seemingly work without pthread however it wont function correctly. libstdc ++有一个不幸的编译和看似没有pthread工作的能力,但它不能正常运行。
See this libstdc++ bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58929 请参阅此libstdc ++错误: https ://gcc.gnu.org/bugzilla/show_bug.cgi?id = 58299
You need to add "-pthread"
to your compile command. 您需要在编译命令中添加
"-pthread"
。
You need to add -pthread
flag to gcc when compiling, for example on gcc 5.1.0 on RE: 你需要在编译时向gcc添加
-pthread
标志,例如在RE上的gcc 5.1.0上:
without pthread: 49752692
with pthread: 2
You need to link to the pthread library using the -pthread
flag to g++: 您需要使用
-pthread
标志链接到pthread库到g ++:
g++ cond_test.cpp -pthread
Most linux systems require you to link to the pthread
library to use threading features. 大多数Linux系统都要求您链接到
pthread
库以使用线程功能。 However programs using standard C++ threading seems to link successfully without explicitly linking to pthread
, and instead produce undefined behavior at runtime (It often crashes, but with this code it seems not to, but instead produce unexpected behavior). 但是,使用标准C ++线程的程序似乎在没有显式链接到
pthread
情况下成功链接,而是在运行时产生未定义的行为(它经常崩溃,但使用此代码似乎不会,但会产生意外行为)。
Example for this code: 此代码的示例:
$ g++ t.cpp && ./a.out
5817437
18860410
$ g++ t.cpp -pthread && ./a.out
2
19718764
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.