-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.h
44 lines (35 loc) · 873 Bytes
/
event.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
//
// event.h
// helper
//
// Created by boss on 2020/10/15.
// Copyright © 2020 boss. All rights reserved.
//
#pragma once
#include <mutex>
#include <condition_variable>
namespace helper {
class Event {
public:
Event() {}
~Event() {}
void Set() {
std::unique_lock<std::mutex> locker(mtx_);
cv_.notify_one();
}
void Wait() {
std::unique_lock<std::mutex> locker(mtx_);
cv_.wait(locker);
}
// 非超时返回true,超时返回false
bool Wait(int milliseconeds) {
std::unique_lock<std::mutex> locker(mtx_);
auto status = cv_.wait_for(locker,
std::chrono::milliseconds(milliseconeds));
return (status != std::cv_status::timeout);
}
private:
std::mutex mtx_;
std::condition_variable cv_;
};
}//end namespace helper