Boost logo

Boost :

Subject: Re: [boost] [threads / signals2] Win events replace by boost signals (threadsafe)
From: james (james_at_[hidden])
Date: 2015-03-26 03:26:07


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_lock<boost::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.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk