c++ - condition_variable::wait_for always waits? -


i have code in unit test waits until vector sufficiently big:

bool waitforoutwardmessages(size_t size, int millis) {     std::unique_lock<std::mutex> lock(mutex);     return ready.wait_for(lock, std::chrono::milliseconds(millis), [=]{         return this->messages.size() >= size;     }); }  std::mutex mutex; std::condition_variable ready; 

easy enough. except when run test, i'm expecting the vector in question should fill on order of milliseconds after make call on other thread. maybe 10ms, maybe 100ms, within 1s. when pass in 5000 millis argument, function always waits 5 seconds.

on 1 hand, fine, because don't care how long test takes anyway. on other hand, thought supposed wait duration if condition variable wasn't notified... not always?

is there way return earlier?

check calling signal or broadcast on ready. there's possible race if you're not careful can signal condition before wait on (which cause wait until timeout).


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -