On 25/03/2015 07:57, Fu ji wrote:
I read about condition variable and it's also not the best idea, because we can receive signal only if we actually wait in this time. For example:
void setcondvar() { boost::this_thread::sleep_for(boost::chrono::milliseconds(200)); std::cout << "Set" << std::endl; cond.notify_all(); std::cout << "After" << std::endl; }
int main() { boost::mutex CondMutex;
boost::thread t1(setcondvar); t1.join();
std::cout << "Before wait" << std::endl;
boost::unique_lockboost::mutex lock(CondMutex); bool result = cond.timed_wait(lock, boost::posix_time::milliseconds(1000));
std::cout << result;
return 0; }
Code that looks like that is wrong. Always have a flag protected by the mutex, always respond to the cond wait returning by checking the flag and waiting again unless timeout.