atomic_output.h (685B)
1 // Copyright 2022 The Chromium Authors. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include <iostream> 6 #include <sstream> 7 #include <string> 8 9 // Utility class to atomically write outout to std::cout. All data streamed 10 // the class is automatically sent to std::cout in the dtor. This is useful 11 // to keep the output of multiple threads writing to std::Cout from 12 // interleaving. 13 14 class AtomicCout { 15 public: 16 ~AtomicCout() { 17 flush(); 18 } 19 20 std::stringstream& stream() { return stream_; } 21 22 void flush() { 23 std::cout << stream_.str(); 24 stream_.str(std::string()); 25 } 26 27 private: 28 std::stringstream stream_; 29 };