From c10e08c3896343cc7ddffe1dd7b1d09da2f8548e Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 7 Jun 2021 20:20:10 +0800 Subject: import(life): ... --- works/life/computer-network-experiment/Output.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'works/life/computer-network-experiment/Output.cpp') 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 +#include #include #include +std::mutex m; + folly::MPMCQueue 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 BlockOutputThread() { + return std::lock_guard(m); +} -- cgit v1.2.3