There are some similar questions on SO. But they only ask the question one way.
std::latch
has an advantage over std::barrier
that unlike latter, former can be decremented by a participating thread more than once.
std::barrier
has an advantage over std::latch
that unlike latter, former can be reused once the arriving threads are unblocked at a phase's synchronization point.
But my question is, why have two almost identical things in the first place? Why they decided not combine both of them into one, something like Java Phaser
?
Phaser
was introduced in Java7, as a more flexible option over CountDownLatch
and CyclicBarrier
, which were introduced in Java5. It has almost identical API as that of both former classes. (Here I took Java's example just to show that combining them is indeed possible.)
Instead of providing a single phaser class, they decided to separately provide latch and barrier, then there must be some benefit from having them separately, mostly some performance related issue. So, what is that issue precisely?