'std::condition_variable::wait_for"经常调用谓词
`std::condition_variable::wait_for` calls the predicate very often
请考虑以下代码片段:
#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
应该是 O(1( 在wait_for
之后(让我们假设虚假解锁很少见(。
但是,我i ~= 3e8
kernel 4.17.14, Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
,i ~= 8e6
kernel 3.10.0, Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz
。
这听起来很有趣,所以我通过与运行 5 秒的简单循环进行比较来检查。i
的结果大致相同,只有5-10%的差异。
问:wait_for
在做什么?它是否按预期工作,我只是理解错误地理解了 cpp首选项,还是我搞砸了?
第二,(可选(问题:i
的巨大差异从何而来?
附加信息: (gcc7.3
,gcc8.2
,clang6.0
(, 标志:-O3 --std=c++17
都产生可比较的结果。
libstdc++有一个不幸的编译能力,似乎在没有pthread的情况下工作,但它将无法正常工作。
请参阅此libstdc++错误:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58929
您需要将"-pthread"
添加到编译命令中。
编译时需要-pthread
标志添加到 gcc,例如在 RE 上的 gcc 5.1.0 上:
without pthread: 49752692
with pthread: 2
你需要使用 g++ 的-pthread
标志链接到 pthread 库:
g++ cond_test.cpp -pthread
大多数 Linux 系统要求您链接到pthread
库才能使用线程功能。然而,使用标准C++线程的程序似乎在没有显式链接到pthread
的情况下成功链接,而是在运行时产生未定义的行为(它经常崩溃,但使用此代码似乎不会,而是产生意外的行为(。
此代码的示例:
$ g++ t.cpp && ./a.out
5817437
18860410
$ g++ t.cpp -pthread && ./a.out
2
19718764
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 函数调用在常量表达式中必须具有常量值
- Singleton模式中的手动析构函数调用:调用多次
- 编译器错误:函数调用在常量表达式中必须有一个常量值
- 如何断言只有当调用对象是常量值时才能调用方法?
- 从同一父调用调用不同的子类函数
- 我可以通过CPP程序中的数据库调用调用Java程序
- NSIS系统::调用-调用方法失败
- 参数构造函数调用调用
- 让非托管 C++ 代码调用调用 C# 代码的托管 C++ 代码
- 如何让函数调用调用它的函数
- 如何p/调用调用函数只使用,但c++需要.a/.lib/.o文件
- C#DLL可以调用调用本机C++静态库的C++/CLI托管包装器吗
- 从"this"中抛弃恒常性,然后更改成员值是否会调用未定义的行为?