I am having a problem understanding why a test case that I think should be passing is failing most of the time. I have distilled the test down to the condition variable and using the wait_for method, specifically testing it to see if it does indeed wait for at least as long as the specified duration.
The test code snippet is below:
TEST_CASE("Condition variable test")
{
std::mutex m;
std::unique_lock<std::mutex> lock(m);
std::condition_variable cv;
bool ex = false;
std::chrono::milliseconds rel_time(50);
auto start = std::chrono::steady_clock::now();
cv.wait_for(lock, rel_time, [&ex] {return(ex);});
auto end = std::chrono::steady_clock::now();
REQUIRE(std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() >= rel_time.count());
}
I would expect from my understanding of the C++11 standard that this should pass all of the time. Ironically, if I change the clock type to the system clock I cannot get the test to fail.
An excerpt from cplusplus.com for the condition_variable::wait_for method states that "If pred is specified (2), the function only blocks if pred returns false, and notifications can only unblock the thread when it becomes true (which is especially useful to check against spurious wake-up calls). It behaves as if implemented as: return wait_until (lck, chrono::steady_clock::now() + rel_time, std::move(pred));"
Which implies to me that using the steady clock to take my reference time stamps is the correct clock.
I am compiling using the MinGW environment with the gcc 4.8.2 compiler.
Aucun commentaire:
Enregistrer un commentaire