VC++和GCC下boost :: condition_variable的不同行为

meg*_*024 4 c++ multithreading boost boost-thread

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

no timeout
elapsed time (ms): 1000
Run Code Online (Sandbox Code Playgroud)

使用GCC 4.8 (在线链接)输出编译的相同代码

timeout
elapsed time (ms): 1000
Run Code Online (Sandbox Code Playgroud)

我的观点是VC++输出不正确,应该是timeout.有没有人no timeout在VC++中有相同的输出(即)?如果是,那么它是Win32实现中的一个错误boost::condition_variable吗?

代码是

#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; 
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*k B 6

如果我们阅读文档,我们会看到:

原子调用lock.unlock()并阻塞当前线程.在rel_time参数指示的时间段过去之后,通过调用this-> notify_one()或this-> notify_all()通知线程将解除阻塞,或者虚假地 ... [强调我的]

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