//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;
};
C++, WaitGroup, like golang sync.WaitGroup
最新推荐文章于 2024-05-13 05:16:19 发布