blob: 8efb52590114768fc737501b686ef94de0a67a37 (
plain)
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
|
#include "Output.h"
#include "folly/CancellationToken.h"
folly::MPMCQueue<Output> output_queue(100);
folly::CancellationSource cancellation_source;
std::thread output_thread(OutputThread);
void PrintOutput(const Output &output) {
switch (output.type) {
case OutputType::Error:
error_stream << output.message;
break;
default:
output_stream << output.message;
break;
}
}
void OutputThread() {
while (true) {
if (cancellation_source.getToken().isCancellationRequested()) {
while (true) {
Output output;
if (output_queue.readIfNotEmpty(output)) {
PrintOutput(output);
} else {
return;
}
}
}
Output output;
if (output_queue.readIfNotEmpty(output))
PrintOutput(output);
}
}
void SignalAndWaitForOutputThreadStop() {
cancellation_source.requestCancellation();
output_thread.join();
}
|