condition_variable获取锁速度慢

condition_variable's speed to acquire lock is low

这个问题是关于 condition_variable.wait() 函数的。我认为它可能不会在收到通知时立即锁定 unique_lock 。让我展示我的代码,你会更好地理解我的测试。

注意:编译器 g++,std=c++14

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <atomic>
#include <future>

using namespace std;

mutex global_mut;
condition_variable global_cond;
atomic<bool> bval;
atomic<int> ival;

void accLock() {
  unique_lock<mutex> lock(global_mut);
  while (!bval.load()) {
    global_cond.wait(lock);
  }
  cout << __PRETTY_FUNCTION__ << " get the lock" << endl;
  ival.store(2, memory_order_release);
  lock.unlock();
}

void getVal() {
  lock_guard<mutex> lock(global_mut);
  cout << __PRETTY_FUNCTION__ << " get the lock with " << ival.load(memory_order_acquire) << endl;
}

int main(int argc, char** argv) {
  bval.store(false);
  ival.store(0, memory_order_release);

  // now my global_cond should be waiting for being notified
  std::future<void> fut = std::async(std::launch::async, accLock);

  // now my global_cond should be awaken and lock global_mut  
  bval.store(true);
  global_cond.notify_one();

  // getVal should be waiting for global_mut to be unlocked
  getVal();
  return 0;
}

理想情况下,我希望我的 accLock 线程首先锁定互斥量并更改 ival,这样 getVal() 可以加载最新的 ival,即 2。我希望看到类似

的输出
void accLock() get the lock
void getVal() get the lock with 2

但实际上,这是

void getVal() get the lock with 0
void accLock() get the lock

显然,这个unique_lock并没有在global_cond中锁定"immediately",让getVal()中的lock_guard先得到互斥。 请问实现我想要的东西的正确方法是什么?我对 condition_variable 的理解正确吗?谢谢。

注意: 我使用 memory_order_acl 并发布,因为我认为这可以帮助我 'correct' 订单。但它不起作用。

当两个线程争用一个互斥体时,哪个线程获得它是任意的。如果您希望一件事在另一件事发生之前发生,您有义务编写代码来实现它。互斥量不会强制执行任何特定的顺序。

如果您不希望 getVal 到 运行 直到另一个线程完成,您必须编写一些代码来等待它完成。您可以使用互斥量和条件变量来执行此操作,但您没有这样做。

一般来说,实施会根据您对其施加的限制尽可能地提高效率。停止调用 getVal 的线程效率低下(因为它的所有代码在缓存中都是热的并且已经被调度),所以实现没有这样做。

该实现无法知道您想要什么,并且低效地做事是没有意义的,希望这可能是您真正想要的但没有告诉它。

请注意,稍后 运行 您可能会得到不同的结果。 ready-to-运行 线程执行的顺序是不可预测的,除非你让它可预测。两个都 线程已准备就绪-运行,因此您不能指望任何特定的可靠排序。这将是实施认为在这种情况下最好的。