Since std::latch
is not in many standard C++ libraries, I tried implementing my own, is it OK from memory ordering perspective or yield
ing?
class Latch {
std::atomic<unsigned> count;
public:
explicit Latch(unsigned cnt)
: count(cnt)
{
}
void arrive_and_wait()
{
assert(count > 0);
count.fetch_sub(1, std::memory_order_release);
while (count.load(std::memory_order_acquire) > 0) {
std::this_thread::yield();
}
}
};
```