On Tue, 01 Sep 2015 18:00:31 -0500
[snip]
> >> I think I got it. Lamda Expression capturing this.
> >> while(!m_stopCV.wait_for(lock, std::chrono::seconds(10),
> >> [this](){return m_stop;}) )
> >
> > This is not right. The return value of the version of
> > std::condition_variable::wait_for() which takes a predicate
> > indicates whether the predicate is true or not when the timeout
> > occurs (it returns false if there was a timeout without the
> > predicate becoming true, otherwise true). You don't want to loop
> > on it again: it already contains its own while loop to guard
> > against spurious wake-ups (see §30.5.1/32). Also, will this code
> > compile without the lambda body being 'return this->m_stop' - I
> > can't remember if there is some implicit application of the this
> > pointer to m_stop in the lambda body but I thought not?
> >
> > In summary, your while loop effectively overrides the timeout. If
> > you don't want a timeout, use plain std::condition_variable::wait().
>
> That's not the behavior I am seeing. It outputs "tick..." every 10
> seconds, as expected.
Then you are outputting the ticks within the while loop. Fine if it is
for debugging purposes, but otherwise at variance with the purpose of
a condition variable. (Your code snippet above contained no code which
printed anything - it just spun on a timeout without any purpose.
More generally, looping on a timeout is also usually pointless,
particularly if the condition is one to end the thread rather than to
do some work. If the condition was one about the availability of work
it would make a bit more sense.)
> I want to loop, because the loop is where the task for the thread is
> done. Unless you are implying that the work goes in the lamda..are
> you?
If you are doing work in the while loop above then it must surely be
wrong to have gratuitous waits on a condition variable related to
something completely different, namely whether a stop flag is set?
> > On your mutex point, you always call
> > std::condition_variable::wait{for|until}() with the mutex locked.
>
> Isn't that what I am doing?
Yes. I wasn't arguing that your code is wrong on this point, but you
asked a question and I answered it. I think you may be conflating this
series of posts with your one containing a request for a code review,
which I haven't read.
[snip]
> > One other consequence of this is that no more
> > than one thread can proceed at a time if you do a broadcast on the
> > condition variable - each thread after the first will block until
> > it can acquire the mutex after the previously acquiring thread has
> > released it.
>
> Only when the broadcast is done? That would be ok, because all they
> are going to do is go and exit. As long as the body of the while loop
> can be executed in more than one thread at a time.
You have lost me on while loops (the while loop above which is dependent
on a timeout occurring and does nothing is only executed one thread at a
time because it executes under the mutex, but I rather think you are
talking about something else). I was just explaining how condition
variables work, as I sensed from your question about mutexes that you
weren't clear. Possibly at some point in whatever while loop you have
you need to release the mutex, and acquire it again before looping.
Chris