> Le 21/04/13 10:27, Michael Marcin a écrit :
>> I recently ran across the need to spawn a thread and wait for it to finish its setup before continuing.
>>
>> The accepted answer seems to be using a mutex and condition variable to achieve this.
>>
>> However that work clutters up the code quite a bit with the implementation details.
>>
>> I came across Java's CountDownLatch which does basically the same work but bundles it up into a tidy package.
>>
>>
> What about using boost::barrier
Of course. I'd forgotten Boost.Thread had that. I wish the standard library had included it.
___
Rob
(Sent from my portable computation engine)
_______________________________________________
Do you really need a real example?
I don't have a real example but again, having two threads calling
latch::count_down on the same latch instance is going to be an headache
for the user, immagine a multi producer/single consumer application
where the consumer as to start when at least N items are ready.
Yes sure they can implement their own Latch taking care of not calling
the count_down on the internal latch, for the matter seeing how hard is
to implement a latch they can even implement one from scratch.
At least throw an exception on an already zeroed latch instead to make
the counter to assume a 2^64 − 1 value (making the wait a blocking
call again).
To the other side can you please tell me why you prefer to keep the
precondition breaking the principle of least surprise? Consider that a
CountDownLatch is an already concept in Java and the count_down in there
is implemented without the precondition.
Regards
Gaetano Mendola
_______________________________________________
Throwing an exception would need the same kind of check, which I want to
avoid.. The single option I think is acceptable is the try_count_down
but before adding it I want to have a valid use case and check how this
try_count_down would make the application code better.
>
> To the other side can you please tell me why you prefer to keep the
> precondition breaking the principle of least surprise? Consider that a
> CountDownLatch is an already concept in Java and the count_down in there
> is implemented without the precondition.
>
Java is not the first language on which these kind of mechanism are
used. I used them long time ago in C.
C++ is not Java. C++ interfaces use to use Require clauses that must be
ensured by the user so that the implementation can be as efficient as
possible.
I have used this terms as their are part of a standard C++ proposal and
the proposal has as I would expect this require clause. BTW, I'm open
and considering to use wait and notify which are more in line with the
C++ current interfaces.
Best,
Vicente
If the buffer is a general purpose buffer the wait on it will return as
soon someone puts stuffs on it. As you know there are multiple way to
achieve the same even without the usage of latch.
I'm fine with leaving the precondition after all people have to read
the manual, I have also see you have added a BOOST_ASSERT( count_ > 0).
About the cyclic_latch instead of the bool to drive the counter reset
I would template the latch using two different policies for reset or
not, then I would expose a typedef of it, something like this:
template <class COUNTER_POLICY>
class Latch {
bool count_down(unique_lock<mutex> &lk)
/// pre_condition (count_ > 0)
{
BOOST_ASSERT(count_ > 0);
if (--count_ == 0)
{
counter_policy_.reset(count_);
++generation_;
lk.unlock();
cond_.notify_all();
return true;
}
return false;
}
private:
COUNTER_POLICY counter_policy_;
};
typedef Latch<NoResetPolicy> latch;
typedef Latch<ResetPolicy> cyclic_latch;
Regards
Gaetano Mendola
Would the base Latch class be public? if not, this is an implementation
detail and is equivalent to having two classes but sharing a common
implementation.
Shouldn't the base latch make use of EBO?
Best,
Vicente
Well, not equivalent because with let say a boolean as parameter you
need to check at runtime what to do (reset the counter or not) this way
with a policy the choose is made a compile time, also implementing it
with a boolean will force you to store anyway the original counter
value, NoResetPolicy to the other side will be an empty class.
> Shouldn't the base latch make use of EBO?
Interesting indeed this should even save that 1 byte in case of
NoResetPolicy.
Regards
Gaetano Mendola
And it's even a vantage due the fact they share the implementation.
Regards
Gaetano Mendola