aboutsummaryrefslogtreecommitdiff
path: root/works/life/computer-network-experiment/Output.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-06-07 20:20:10 +0800
committercrupest <crupest@outlook.com>2021-06-07 20:20:10 +0800
commitc10e08c3896343cc7ddffe1dd7b1d09da2f8548e (patch)
tree9a62f500d177a4190a4c3b642a58a38e109e56ea /works/life/computer-network-experiment/Output.cpp
parent04bc88aca6157dfd1d2309b353d9feb64bcc8800 (diff)
downloadcrupest-c10e08c3896343cc7ddffe1dd7b1d09da2f8548e.tar.gz
crupest-c10e08c3896343cc7ddffe1dd7b1d09da2f8548e.tar.bz2
crupest-c10e08c3896343cc7ddffe1dd7b1d09da2f8548e.zip
import(life): ...
Diffstat (limited to 'works/life/computer-network-experiment/Output.cpp')
-rw-r--r--works/life/computer-network-experiment/Output.cpp11
1 files changed, 11 insertions, 0 deletions
diff --git a/works/life/computer-network-experiment/Output.cpp b/works/life/computer-network-experiment/Output.cpp
index 2989c98..db97e5e 100644
--- a/works/life/computer-network-experiment/Output.cpp
+++ b/works/life/computer-network-experiment/Output.cpp
@@ -2,9 +2,12 @@
#include <folly/CancellationToken.h>
+#include <mutex>
#include <ostream>
#include <type_traits>
+std::mutex m;
+
folly::MPMCQueue<Output> output_queue(100);
folly::CancellationSource cancellation_source;
@@ -44,6 +47,8 @@ void PrintOutput(const Output &output) {
void OutputThread() {
while (true) {
+ m.lock();
+
if (cancellation_source.getToken().isCancellationRequested()) {
while (true) {
Output output;
@@ -58,6 +63,8 @@ void OutputThread() {
Output output;
if (output_queue.readIfNotEmpty(output))
PrintOutput(output);
+
+ m.unlock();
}
}
@@ -65,3 +72,7 @@ void SignalAndWaitForOutputThreadStop() {
cancellation_source.requestCancellation();
output_thread.join();
}
+
+std::lock_guard<std::mutex> BlockOutputThread() {
+ return std::lock_guard<std::mutex>(m);
+}