Skip to content

Commit

Permalink
添加 CountDownLatch
Browse files Browse the repository at this point in the history
添加 CountDownLatch, 批量等待多个Coroutine结束
  • Loading branch information
zhcpku committed Sep 1, 2021
1 parent 4780002 commit 20be255
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions libgo/coroutine.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#define __const__
#include <atomic>
#include "common/config.h"
#include "common/pp.h"
#include "common/syntax_helper.h"
Expand Down Expand Up @@ -71,3 +72,29 @@ typedef ::co::CoTimer::TimerId co_timer_id;
#define co_last_defer() ::co::GetLastDefer()
#define co_defer_scope co_defer [&]

class CountDownLatch {
public:
explicit CountDownLatch(size_t n = 1) : mFlyingCount(n) {}

void Add(size_t i) {
mFlyingCount += i;
}

void Done() {
if (--mFlyingCount == 0) {
ch_0.Close();
}
}
void Wait() {
int done;
ch_0 >> done;
}
private:
std::atomic<size_t> mFlyingCount;
co_chan<int> ch_0;

CountDownLatch(CountDownLatch const &) = delete;
CountDownLatch(CountDownLatch &&) = delete;
CountDownLatch& operator=(CountDownLatch const &) = delete;
CountDownLatch& operator=(CountDownLatch &&) = delete;
};

0 comments on commit 20be255

Please sign in to comment.