-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.cc
49 lines (44 loc) · 972 Bytes
/
stream.cc
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
#include <stream.h>
#include <sys/prctl.h>
std::atomic<int> Stream::streamId(0);
Stream::Stream() {
isRunning = true;
apiNum = 0;
completeNum = 0;
curId = streamId;
streamId.fetch_add(1);
workThread = std::thread(&Stream::loop, this);
}
Stream::~Stream() {
isRunning = false;
sync();
workThread.join();
}
void Stream::loop() {
prctl(PR_SET_NAME, std::to_string(curId).c_str());
while (1) {
std::function<void()> api;
{
std::unique_lock<std::mutex> lock(mtx);
if (!isRunning) {
return;
}
if (apiQueue.empty()) {
lock.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(5));
continue;
}
api = std::move(apiQueue.front());
apiQueue.pop();
}
api();
++completeNum;
syncCV.notify_one();
}
return;
}
void Stream::sync() {
std::unique_lock<std::mutex> lock(mtx);
syncCV.wait(lock, [this] { return apiNum == completeNum; });
return;
}