`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 应该是 wait_for 之后的 O(1)(假设虚假解锁很少见)。

然而,我得到
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 在做什么?它是否按预期工作,我只是理解错了 cppreference,还是我搞砸了?

第二,(可选)问题:i 的巨大差异从何而来?

附加信息: (gcc7.3gcc8.2clang6.0)、标志:-O3 --std=c++17 都产生了可比较的结果。

你需要在编译时给gcc添加-pthread标志,例如在RE上的gcc 5.1.0:

without pthread: 49752692
with pthread: 2

您需要 link 使用 g++ 的 -pthread 标志到 pthread 库:

g++ cond_test.cpp -pthread

大多数 linux 系统需要您 link 到 pthread 库才能使用线程功能。然而,使用标准 C++ 线程的程序似乎 link 成功而无需显式 linking 到 pthread,而是在运行时产生未定义的行为(它经常崩溃,但使用这段代码似乎不会,而是产生意想不到的行为)。

此代码的示例:

$ g++ t.cpp  && ./a.out
5817437
18860410
$ g++ t.cpp  -pthread && ./a.out
2
19718764

libstdc++ 有一个不幸的编译能力,并且在没有 pthread 的情况下似乎可以工作,但它不会正常运行。

查看此 libstdc++ 错误:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58929

您需要在编译命令中添加"-pthread"