When calling the notify_once method on a condition variable within a loop in one thread, it appears that the wait method in another thread only checks it predicate once. Example code is listed below:
int someNumber = 0;
mutex numberMutex;
condition_variable cv;
void waitFunction()
{
unique_lock<mutex> lock(numberMutex);
auto duration = chrono::seconds(5);
// Predicate is only called once?
cv.wait_for(lock, duration, []() {
cout << "Checking condition: 10 == " << someNumber << '\n';
return someNumber == 10;
});
cout << "Done with this thread...\n" << flush;
}
int main()
{
thread waiter(waitFunction);
for (size_t number = 0; number != 50; ++number)
{
{
lock_guard<mutex> guard(numberMutex);
someNumber = number;
}
cv.notify_one();
}
waiter.join();
}
Executing this program gives the following output
Checking condition: 10 == 49
Checking condition: 10 == 49
Done with this thread...
while I would expect it to check the condition at each iteration.
How is this possible?
Aucun commentaire:
Enregistrer un commentaire