aboutsummaryrefslogtreecommitdiff
path: root/computer-network-experiment/Output.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-06-07 14:05:51 +0800
committercrupest <crupest@outlook.com>2021-06-07 14:05:51 +0800
commit6774bcc32ac23728827b36a8c2c2350e811f7940 (patch)
treeee55e8f7ca3ef7c937dd797f863016dfb740e21b /computer-network-experiment/Output.cpp
parent7f2badbf713d37a75df03c1e0704ddab6af06627 (diff)
downloadlife-6774bcc32ac23728827b36a8c2c2350e811f7940.tar.gz
life-6774bcc32ac23728827b36a8c2c2350e811f7940.tar.bz2
life-6774bcc32ac23728827b36a8c2c2350e811f7940.zip
...
Diffstat (limited to 'computer-network-experiment/Output.cpp')
-rw-r--r--computer-network-experiment/Output.cpp43
1 files changed, 34 insertions, 9 deletions
diff --git a/computer-network-experiment/Output.cpp b/computer-network-experiment/Output.cpp
index 2968c19..8efb525 100644
--- a/computer-network-experiment/Output.cpp
+++ b/computer-network-experiment/Output.cpp
@@ -1,18 +1,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) {
- Output output;
- output_queue.blockingRead(output);
- switch (output.type) {
- case OutputType::Error:
- error_stream << output.message;
- break;
- default:
- output_stream << output.message;
- break;
+ 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();
+}