-
Notifications
You must be signed in to change notification settings - Fork 1
/
count_down_latch.h
57 lines (51 loc) · 1.5 KB
/
count_down_latch.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma once
#include <mutex>
#include <chrono>
class count_down_latch
{
private:
std::mutex m_;
std::condition_variable cv_;
long count_;
public:
explicit count_down_latch(const int count) :count_(count) {};
~count_down_latch();
void await(); //Causes the current thread to wait until the latch has counted down to zero.
template <typename Rep, typename Period>
bool await(std::chrono::duration<Rep, Period> timeout); //Causes the current thread to wait until the latch has counted down to zero, unless the specified waiting time elapses.
void count_down(); //Decrements the count of the latch, releasing all waiting threads if the count reaches zero.
long count(); //Returns the current count.
};
inline count_down_latch::~count_down_latch()
{
std::lock_guard<std::mutex> lg(m_);
count_ = 0;
cv_.notify_all();
}
inline void count_down_latch::await()
{
std::unique_lock<std::mutex> ul(m_);
if (count_ <= 0) return;
cv_.wait(ul, [this]() {return count_ == 0; });
}
template <typename Rep, typename Period>
bool count_down_latch::await(std::chrono::duration<Rep, Period> timeout)
{
std::unique_lock<std::mutex> ul(m_);
if (count_ <= 0) return true;
cv_.wait_for(ul, timeout, [this]() {return count_ == 0; });
if (count != 0)
return false;
return true;
}
inline void count_down_latch::count_down()
{
std::lock_guard<std::mutex> lg(m_);
count_--;
if (count_ <= 0) cv_.notify_all();
}
inline long count_down_latch::count()
{
std::lock_guard<std::mutex> lg(m_);
return count_;
}