【问题标题】:Different behavior of boost::condition_variable under VC++ and GCCVC++ 和 GCC 下 boost::condition_variable 的不同行为
【发布时间】:2013-04-16 18:08:35
【问题描述】:

在我的计算机上,在 Windows 7 上运行,以下代码在 Visual C++ 2010 和 Boost 1.53 中编译,输出

no timeout
elapsed time (ms): 1000

使用 GCC 4.8 编译的相同代码 (online link) 输出

timeout
elapsed time (ms): 1000

我的看法是VC++输出不正确,应该是timeout。有没有人在 VC++ 中有相同的输出(即no timeout)?如果是,那么这是boost::condition_variable的Win32实现中的错误吗?

代码是

#include <boost/thread.hpp>
#include <iostream>

int main(void) {
  boost::condition_variable cv;
  boost::mutex mx;
  boost::unique_lock<decltype(mx)> lck(mx);
  boost::chrono::system_clock::time_point start = boost::chrono::system_clock::now();
  const auto cv_res = cv.wait_for(lck, boost::chrono::milliseconds(1000));
  boost::chrono::system_clock::time_point end = boost::chrono::system_clock::now();
  const auto count = (boost::chrono::duration_cast<boost::chrono::milliseconds>(end - start)).count();
  const std::string str = (cv_res == boost::cv_status::no_timeout) ? "no timeout" : "timeout";
  std::cout << str << std::endl;
  std::cout << "elapsed time (ms): " << count << std::endl;
  return 0; 
}

【问题讨论】:

    标签: c++ multithreading boost boost-thread


    【解决方案1】:

    如果我们阅读我们看到的文档:

    以原子方式调用 lock.unlock() 并阻塞当前线程。这 当调用 this->notify_one() 或通知时,线程将解除阻塞 this->notify_all(),在 rel_time 指示的时间段之后 争论已经过去,或虚假地...[强调我的]

    您几乎可以肯定看到的是,VS 实现将其视为虚假唤醒,恰好在预期持续时间结束时,而其他实现将其视为超时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      • 1970-01-01
      • 2021-03-09
      • 2021-03-04
      • 1970-01-01
      相关资源
      最近更新 更多