Different boost :: condition_variable behavior under VC ++ and GCC

On my computer running Windows 7, the following code compiled in Visual C ++ 2010 with Boost 1.53 displays

no timeout elapsed time (ms): 1000 

The same code compiled with GCC 4.8 (online link ) outputs

 timeout elapsed time (ms): 1000 

My opinion is that the output of VC ++ is incorrect, and it should be timeout . Does anyone have the same result (i.e. no timeout ) in VC ++? If so, is this an error in the implementation of Win32 boost::condition_variable ?

Code

 #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; } 
+4
source share
1 answer

If we read the documentation, we see:

Call lock.unlock () atomically and locks the current thread. the stream will be unblocked upon notification of a call to this-> notify_one () or this-> notify_all (), after a period of time specified by rel_time the argument has expired, or false ... [emphasis mine]

What you almost certainly see is that the VS implementation sees it as a false awakening, which turns out to be at the end of the expected duration, while the other implementation sees it as a timeout.

+6
source

All Articles