<tfoot id='AgtVr'></tfoot>
        <bdo id='AgtVr'></bdo><ul id='AgtVr'></ul>
    1. <legend id='AgtVr'><style id='AgtVr'><dir id='AgtVr'><q id='AgtVr'></q></dir></style></legend>

    2. <i id='AgtVr'><tr id='AgtVr'><dt id='AgtVr'><q id='AgtVr'><span id='AgtVr'><b id='AgtVr'><form id='AgtVr'><ins id='AgtVr'></ins><ul id='AgtVr'></ul><sub id='AgtVr'></sub></form><legend id='AgtVr'></legend><bdo id='AgtVr'><pre id='AgtVr'><center id='AgtVr'></center></pre></bdo></b><th id='AgtVr'></th></span></q></dt></tr></i><div id='AgtVr'><tfoot id='AgtVr'></tfoot><dl id='AgtVr'><fieldset id='AgtVr'></fieldset></dl></div>

    3. <small id='AgtVr'></small><noframes id='AgtVr'>

      如何轻松使 std::cout 线程安全?

      时间:2023-09-18
        • <bdo id='asGPP'></bdo><ul id='asGPP'></ul>
          <i id='asGPP'><tr id='asGPP'><dt id='asGPP'><q id='asGPP'><span id='asGPP'><b id='asGPP'><form id='asGPP'><ins id='asGPP'></ins><ul id='asGPP'></ul><sub id='asGPP'></sub></form><legend id='asGPP'></legend><bdo id='asGPP'><pre id='asGPP'><center id='asGPP'></center></pre></bdo></b><th id='asGPP'></th></span></q></dt></tr></i><div id='asGPP'><tfoot id='asGPP'></tfoot><dl id='asGPP'><fieldset id='asGPP'></fieldset></dl></div>

              <small id='asGPP'></small><noframes id='asGPP'>

              <tfoot id='asGPP'></tfoot>
            1. <legend id='asGPP'><style id='asGPP'><dir id='asGPP'><q id='asGPP'></q></dir></style></legend>
                <tbody id='asGPP'></tbody>

                本文介绍了如何轻松使 std::cout 线程安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个多线程应用程序,它大量使用 std::cout 进行日志记录而没有任何锁定.在这种情况下,如何轻松添加锁机制使 std::cout 成为线程安全的?

                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 std::cout thread-safe?

                我不想搜索每次出现的 std::cout 并添加一行锁定代码.太麻烦了.

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

                有什么更好的做法吗?

                推荐答案

                注意:这个答案是 pre-C++20 所以它不使用 std::osyncstream 具有单独的缓冲,但使用锁代替.

                我猜您可以实现自己的类,该类包装 cout 并将互斥体与其关联.那个新类的 operator << 会做三件事:

                I guess you could implement your own class which wraps cout and associates a mutex with it. The operator << of that new class would do three things:

                1. 为互斥锁创建一个锁,可能会阻塞其他线程
                2. 执行输出,即对包装的流和传递的参数执行操作符 <<
                3. 构造一个不同类的实例,将锁传递给那个
                1. create a lock for the mutex, possibly blocking other threads
                2. do the output, i.e. do the operator << for the wrapped stream and the passed argument
                3. construct an instance of a different class, passing the lock to that

                这个不同的类将把锁和委托操作符 << 保留到包装的流中.第二个类的析构函数最终会破坏锁并释放互斥锁.

                This different class would keep the lock and delegate operator << to the wrapped stream. The destructor of that second class would eventually destroy the lock and release the mutex.

                因此,您作为单个语句编写的任何输出,即作为 << 调用的单个序列,只要您的所有输出通过具有相同互斥量的对象,就会自动打印.

                So any output you write as a single statement, i.e. as a single sequence of << invocations, will be printed atomically as long as all your output goes through that object with the same mutex.

                让我们调用两个类 synchronized_ostreamlocked_ostream.如果 sync_coutsynchronized_ostream 的一个实例,它包装了 std::cout,那么序列

                Let's call the two classes synchronized_ostream and locked_ostream. If sync_cout is an instance of synchronized_ostream which wraps std::cout, then the sequence

                sync_cout << "Hello, " << name << "!" << std::endl;
                

                将导致以下操作:

                1. synchronized_ostream::operator<<< 会获得锁
                2. synchronized_ostream::operator<< 会将Hello,"的打印委托给 cout
                3. operator<<(std::ostream&, const char*) 将打印Hello,"
                4. synchronized_ostream::operator<< 会构造一个 locked_ostream 并将锁传递给那个
                5. locked_ostream::operator<< 会将name 的打印委托给cout
                6. operator<<(std::ostream&, std::string) 将打印名称
                7. 同样的委托给 cout 发生在感叹号和结束线操纵器
                8. locked_ostream 临时被破坏,锁被释放
                1. synchronized_ostream::operator<< would aquire the lock
                2. synchronized_ostream::operator<< would delegate the printing of "Hello, " to cout
                3. operator<<(std::ostream&, const char*) would print "Hello, "
                4. synchronized_ostream::operator<< would construct a locked_ostream and pass the lock to that
                5. locked_ostream::operator<< would delegate the printing of name to cout
                6. operator<<(std::ostream&, std::string) would print the name
                7. The same delegation to cout happens for the exclamation point and the endline manipulator
                8. The locked_ostream temporary gets destructed, the lock is released

                这篇关于如何轻松使 std::cout 线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在 C++ 中读取不断增长的文本文件? 下一篇:无论如何将以下内容编写为 C++ 宏?

                相关文章

                最新文章

              • <small id='6RrLB'></small><noframes id='6RrLB'>

                <tfoot id='6RrLB'></tfoot>
                <i id='6RrLB'><tr id='6RrLB'><dt id='6RrLB'><q id='6RrLB'><span id='6RrLB'><b id='6RrLB'><form id='6RrLB'><ins id='6RrLB'></ins><ul id='6RrLB'></ul><sub id='6RrLB'></sub></form><legend id='6RrLB'></legend><bdo id='6RrLB'><pre id='6RrLB'><center id='6RrLB'></center></pre></bdo></b><th id='6RrLB'></th></span></q></dt></tr></i><div id='6RrLB'><tfoot id='6RrLB'></tfoot><dl id='6RrLB'><fieldset id='6RrLB'></fieldset></dl></div>

                    <bdo id='6RrLB'></bdo><ul id='6RrLB'></ul>
                    <legend id='6RrLB'><style id='6RrLB'><dir id='6RrLB'><q id='6RrLB'></q></dir></style></legend>