How to easily make std::cout thread-safe?

前端 未结 9 709
庸人自扰
庸人自扰 2020-12-02 10:52

I have a multi-threaded application, which heavily uses std::cout for logging without any locking. In such a case, how can I easily add lock mechanism to make <

相关标签:
9条回答
  • 2020-12-02 10:55

    For fast debugging c++11 applications and avoid interleaved output I just write small functions like these:

    ...
    #include <mutex>
    ...
    mutex m_screen;
    ...
    void msg(char const * const message);
    ...
    void msg(char const * const message)
    {
      m_screen.lock();
      cout << message << endl;
      m_screen.unlock();
    }
    

    I use these types of functions for outputs and if numeric values are needed I just use something like this:

    void msgInt(char const * const message, int const &value);
    ...
    void msgInt(char const * const message, int const &value)
    {
      m_screen.lock();
      cout << message << " = " << value << endl;
      m_screen.unlock();
    }
    

    This is easy and works fine to me, but I don't really know if it is technically correct. So I would be glad to hear your opinions.


    Well, I didn't read this:

    I don't want to search for each occurrence of std::cout and add a line of locking code.

    I'm sorry. However I hope it helps somebody.

    0 讨论(0)
  • 2020-12-02 11:04

    I know its an old question, but it helped me a lot with my problem. I created an utility class based on this post answers and I'd like to share my result.

    Considering we use C++11 or latter C++ versions, this class provides print and println functions to compose strings before calling the standard output stream and avoid concurrency problems. These are variadic functions which use templates to print different data types.

    You can check its use in a producer-consumer problem on my github: https://github.com/eloiluiz/threadsBar

    So, here is my code:

    class Console {
    private:
        Console() = default;
    
        inline static void innerPrint(std::ostream &stream) {}
    
        template<typename Head, typename... Tail>
        inline static void innerPrint(std::ostream &stream, Head const head, Tail const ...tail) {
            stream << head;
            innerPrint(stream, tail...);
        }
    
    public:
        template<typename Head, typename... Tail>
        inline static void print(Head const head, Tail const ...tail) {
            // Create a stream buffer
            std::stringbuf buffer;
            std::ostream stream(&buffer);
            // Feed input parameters to the stream object
            innerPrint(stream, head, tail...);
            // Print into console and flush
            std::cout << buffer.str();
        }
    
        template<typename Head, typename... Tail>
        inline static void println(Head const head, Tail const ...tail) {
            print(head, tail..., "\n");
        }
    };
    
    0 讨论(0)
  • 2020-12-02 11:06

    In addition to synchronisation this solution provides information about the thread from which the log was written.

    DISCLAIMER: It's quite a naive way of syncing the logs, however it might be applicable for some small use cases for debugging.

    thread_local int thread_id = -1;
    std::atomic<int> thread_count;
    
    struct CurrentThread {
    
      static void init() {
        if (thread_id == -1) {
          thread_id = thread_count++;
        }
      }
    
      friend std::ostream &operator<<(std::ostream &os, const CurrentThread &t) {
        os << "[Thread-" << thread_id << "] - ";
        return os;
      }
    };
    
    CurrentThread current_thread;
    std::mutex io_lock;
    #ifdef DEBUG
    #define LOG(x) {CurrentThread::init(); std::unique_lock<std::mutex> lk(io_lock); cout << current_thread << x << endl;}
    #else
    #define LOG(x)
    #endif
    

    This can be used like this.

    LOG(cout << "Waiting for some event");
    

    And it will give log output

    [Thread-1] - Entering critical section 
    [Thread-2] - Waiting on mutex
    [Thread-1] - Leaving critical section, unlocking the mutex
    
    0 讨论(0)
  • 2020-12-02 11:07

    While I can't be sure this applies to every compiler / version of std libs but in the code-base I'm using std::cout::operator<<() it is already thread-safe.

    I'm assuming that what you're really trying to do it stop std::cout from mixing string when concatenating with the operator<< multiple time per string, across multiple threads.

    The reason strings get garbled is because there is a "External" race on the operator<< this can lead to things like this happening.

    //Thread 1
    std::cout << "the quick brown fox " << "jumped over the lazy dog " << std::endl;
    
    //Thread 2
    std::cout << "my mother washes" << " seashells by the sea shore" << std::endl;
    
    //Could just as easily print like this or any other crazy order.
    my mother washes the quick brown fox seashells by the sea shore \n
    jumped over the lazy dog \n
    

    If that's the case there is a much simpler answer than making your own thread safe cout or implementing a lock to use with cout.

    Simply compose your string before you pass it to cout

    For example.

    //There are other ways, but stringstream uses << just like cout.. 
    std::stringstream msg;
    msg << "Error:" << Err_num << ", " << ErrorString( Err_num ) << "\n"; 
    std::cout << msg.str();
    

    This way your stings can't be garbled because they are already fully formed, plus its also a better practice to fully form your strings anyway before dispatching them.

    0 讨论(0)
  • 2020-12-02 11:10

    Since C++20, you can use std::osyncstream wrapper:

    http://en.cppreference.com/w/cpp/io/basic_osyncstream

    {
      std::osyncstream bout(std::cout); // synchronized wrapper for std::cout
      bout << "Hello, ";
      bout << "World!";
      bout << std::endl; // flush is noted, but not yet performed
      bout << "and more!\n";
    } // characters are transferred and std::cout is flushed
    

    It provides the guarantee that all output made to the same final destination buffer (std::cout in the examples above) will be free of data races and will not be interleaved or garbled in any way, as long as every write to the that final destination buffer is made through (possibly different) instances of std::basic_osyncstream.

    0 讨论(0)
  • 2020-12-02 11:14

    I really like the trick from Nicolás given in this question of creating a temporary object and putting the protection code on the destructor.

    /** Thread safe cout class
      * Exemple of use:
      *    PrintThread{} << "Hello world!" << std::endl;
      */
    class PrintThread: public std::ostringstream
    {
    public:
        PrintThread() = default;
    
        ~PrintThread()
        {
            std::lock_guard<std::mutex> guard(_mutexPrint);
            std::cout << this->str();
        }
    
    private:
        static std::mutex _mutexPrint;
    };
    
    std::mutex PrintThread::_mutexPrint{};
    

    You can then use it as a regular std::cout, from any thread:

    PrintThread{} << "my_val=" << val << std::endl;
    

    The object collect data as a regular ostringstream. As soon the coma is reached, the object is destroyed and flush all collected information.

    0 讨论(0)
提交回复
热议问题