undefined

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//C++ WaitGroup like golang sync.WaitGroup
class WaitGroup {
public:
void Add(int incr = 1) { counter += incr; }
void Done() { if (--counter <= 0) cond.notify_all(); }
void Wait() {
std::unique_lock<std::mutex> lock(mutex);
cond.wait(lock, [&] { return counter <= 0; });
}

private:
std::mutex mutex;
std::atomic<int> counter;
std::condition_variable cond;
};