Boost logo

Boost :

Subject: Re: [boost] [thread] countdown_latch
From: Michael Marcin (mike.marcin_at_[hidden])
Date: 2013-05-15 23:38:39

On 5/15/2013 12:10 PM, Gaetano Mendola wrote:
> On 14/05/2013 22.11, Michael Marcin wrote:
>> On 5/14/13 2:21 PM, Gaetano Mendola wrote:
>>> Given it has a clearly inspiration on Java one I would leave it like it
>>> it now with the excpetion to permit count_down even if the counter is
>>> zero already.
>> I've seen that the Java one supports that, but most c++ implementations
>> seem to not.
>> What is the purpose of allowing count_down when the counter is already
>> zero? What useful functionality does it enable?
> It will permit a thread T1 to proceed if another thread T2 have executed
> a certain operation at least N times, as it is implemented now
> (with the precondition of count_down that counter > 0) T2 has to track
> how many count_down it has executed (knowing then the initial value) or
> issue a "try_wait" before the count_down and this IMHO seems a bit
> awkward.

You say it will permit T1 to proceed, this means T1 is calling wait,
right? Wait does not have the precondition that counter > 0.

Oh I see, you're saying T2 which does call count_down has to know how
many count_downs it is safe to do? The more common use case I figured
was N threads all calling count_down once.

I suppose this use case is just as valid. Still it seems error prone to
me to have this be the default. You could easily have a situation where
you spawn 5 threads to do work and want to wait for them all the be
initialized before any starts doing work, then a later maintainer comes
along and adds a 6th worker thread without updating the latch count.

Now the latch would reach zero and the threads would start doing their
work before the 6th is necessarily initialized and could lead to hard to
trackdown bugs.

Perhaps you could add a try_count_down method which removes doesn't have
the counter > 0 precondition?

Boost list run by bdawes at, gregod at, cpdaniel at, john at