3 ms·
Nice writeup as usual by Chris, but I'm pretty sure there were public non-broken, efficient spin-only barrier implementations before this. Here's one I wrote:
by BeeOnRope 5y ago
Nice writeup as usual by Chris, but I'm pretty sure there were public non-broken, efficient spin-only barrier implementations before this.
Here's one I wrote:
https://github.com/travisdowns/concurrency-hierarchy-bench/blob/30f30a7e37f2433c21528942ba7d27824be1d1b4/cyclic-barrier.hpp#L15 https://github.com/travisdowns/concurrency-hierarchy-bench/b...
Now it is a lot larger and CAS based, rather than an unconditional increment, but that was to support the extra feature of having the "breaker" run a function and return the value, which I needed in this context, as well as reporting the number of spins, etc. The earlier iterations had an unconditional increment.
The idea is substantially the same as Chris' and uses only 1 word of state (though my implementation stores the n, the break count, as a member, you could pass it in as Chris does): but it uses the sign bit rather than the (log(n) + 1)th bit to indicate the phase: the count goes negative when in the "draining" phase after the barrier is broken. It supports arbitrary break counts without division and the fast path is comparable to the one in the post, though "fast path" is kind of meaningless when you expect to spin most of the time (as long as you don't invoke the scheduler, at least).
That said, Chris's bit manipulation approach is very compact and elegant!