Skip to content

Commit d0d5d42

Browse files
authored
添加 CountDownLatch
添加了 CountDownLatch,一个类似 golang 中 sync.WaitGroup 的工具类
1 parent 4780002 commit d0d5d42

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

libgo/coroutine.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,21 @@ typedef ::co::CoTimer::TimerId co_timer_id;
7171
#define co_last_defer() ::co::GetLastDefer()
7272
#define co_defer_scope co_defer [&]
7373

74+
class CountDownLatch {
75+
public:
76+
explicit CountDownLatch(size_t n = 1) : mFlyingCount(n) {}
77+
78+
void Done() {
79+
--mFlyingCount;
80+
}
81+
void Wait() {
82+
while (mFlyingCount) {
83+
co_yield;
84+
}
85+
}
86+
private:
87+
size_t mFlyingCount;
88+
89+
CountDownLatch(CountDownLatch const &) = delete;
90+
CountDownLatch& operator=(CountDownLatch const &) = delete;
91+
};

0 commit comments

Comments
 (0)