diff options
author | crupest <crupest@outlook.com> | 2021-06-07 14:05:51 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-06-07 14:05:51 +0800 |
commit | da6c0e6194578538ce0bcd1b9815696b96153f6b (patch) | |
tree | bb4121f622161fad8810890e0e16aac8c1826605 /works/life/computer-network-experiment/Output.cpp | |
parent | 2de4663d385ab54c5d8b8adc68611ad67636f56f (diff) | |
download | crupest-da6c0e6194578538ce0bcd1b9815696b96153f6b.tar.gz crupest-da6c0e6194578538ce0bcd1b9815696b96153f6b.tar.bz2 crupest-da6c0e6194578538ce0bcd1b9815696b96153f6b.zip |
import(life): ...
Diffstat (limited to 'works/life/computer-network-experiment/Output.cpp')
-rw-r--r-- | works/life/computer-network-experiment/Output.cpp | 43 |
1 files changed, 34 insertions, 9 deletions
diff --git a/works/life/computer-network-experiment/Output.cpp b/works/life/computer-network-experiment/Output.cpp index 2968c19..8efb525 100644 --- a/works/life/computer-network-experiment/Output.cpp +++ b/works/life/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();
+}
|