简体   繁体   English

C ++多线程程序I / O问题与std :: endl

[英]C++ multithread program I/O problem with std::endl

I try to learn how to use C++11 thread library and then, I am confused about the output of my following code. 我尝试学习如何使用C ++ 11线程库,然后对以下代码的输出感到困惑。

#include <iostream>
#include <thread>
#include <mutex>

std::mutex mtx;

void thread_function()
{
    std::cout << "Inside Thread :: ID = " << std::this_thread::get_id() << std::endl;
}

int main()
{
    std::thread threadObj1(thread_function);
    std::thread threadObj2(thread_function);

    if (threadObj1.get_id() != threadObj2.get_id())
        std::cout << "Both threads have different id" << std::endl;

    threadObj1.join();
    threadObj2.join();
    std::cout<<"From Main Thread :: ID of Thread 1 = "<<threadObj1.get_id()<<std::endl;
    std::cout<<"From Main Thread :: ID of Thread 2 = "<<threadObj2.get_id()<<std::endl;


    return 0;
}

I attach every std::cout with std::endl in order to flush the buffer and output the '/n' character. 我将每个std :: cout附加到std :: endl以便刷新缓冲区并输出'/ n'字符。 However, finally I got the output as the following. 但是,最终我得到如下输出。

Both threads have different idInside Thread :: ID = Inside Thread :: ID = 
0x700003715000
0x700003692000
From Main Thread :: ID of Thread 1 = 0x0
From Main Thread :: ID of Thread 2 = 0x0
Program ended with exit code: 0

It seems that the '/n' before the Inside Thread disappeared. 似乎内部线程之前的“ / n”消失了。 Could you please help me figure it out? 你能帮我弄清楚吗? Thank you so much! 非常感谢!

You have 3 threads which are accessing cout without any synchronization. 您有3个线程在没有任何同步的情况下访问cout You have defined mtx but it is not used, why? 您已定义mtx但未使用它,为什么?

Add lock_guard to protect cout statement: 添加lock_guard以保护cout语句:

void thread_function()
{
    std::lock_guard<std::mutex> lk{mtx};
    std::cout << "Inside Thread :: ID = " << std::this_thread::get_id() << std::endl;
}

    if (threadObj1.get_id() != threadObj2.get_id())
    {
        std::lock_guard<std::mutex> lk{mtx};
        std::cout << "Both threads have different id" << std::endl;
    }

I think I should also receive three '\\n' right? 我想我也应该收到三个'\\ n'对吗?

The three '\\n' characters in question are there in your output. 有关的三个'\\n'字符在您的输出中。 They're at the ends of the first three lines of output. 它们位于输出的前三行的末尾。

I think maybe you misunderstand what this line from your example means: 我认为您可能会误解示例中的这一行是什么意思:

std::cout << "Inside Thread :: ID = " << std::this_thread::get_id() << std::endl;

There are four separate function calls explicit in that one line of code. 一行代码中有四个单独的函数调用。 That one line does exactly the same thing as these four lines: 那一行与这四行完全相同:

std::cout << "Inside Thread :: ID = ";
auto id = std::this_thread::get_id();
std::cout << id;
std::cout << std::endl;

Even assuming that the std::cout object is fully synchronized, You have done nothing to prevent the various threads from interleaving the separate function calls. 即使假设std::cout对象已完全同步,您也没有采取任何措施来防止各种线程交错单独的函数调用。 Eg, 例如,

  • main thread calls std::cout << "Both threads have different id"; 主线程调用std::cout << "Both threads have different id";
  • threadObj1 calls std::cout << "Inside Thread :: ID = "; threadObj1调用std::cout << "Inside Thread :: ID = ";
  • threadObj2 calls std::cout << "Inside Thread :: ID = "; threadObj2调用std::cout << "Inside Thread :: ID = ";
  • main thread calls std::cout << std::endl; 主线程调用std::cout << std::endl;
  • threadObj1 calls std::cout << std::this_thread::get_id(); threadObj1调用std::cout << std::this_thread::get_id();
  • threadObj1 calls stc::cout << std::endl; threadObj1调用stc::cout << std::endl;
  • etc. 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM