线程库的奇怪行为

Strange behaviour of the thread library

本文关键字:线程      更新时间:2023-10-16

我不知道这个话题是否与 std::thread 库或流有关。请看以下示例:

#include <thread>
#include <iostream>
void read(){
    int bar;
    std::cout << "Enter an int: ";
    std::cin >> bar;
}
void print(){
    std::cout << "foo";
}
int main(){
    std::thread rT(read);
    std::thread pT(print);
    rT.join();
    pT.join();
    return 0;
}

我不在乎它会在执行 read() 函数之前还是之后打印"foo"字符串。困扰我的事实是,当它在执行 print() 函数之前请求输入时,它实际上会挂起执行。我必须单击"输入"或向 std::cin 提供一些数据才能看到"foo"字符串。您可以在下面看到该程序行为的三种可能场景:

1.
>> Enter an int: //here I've clicked enter
>> foo
>> 12 //here I've written "12" and clicked enter
//end of execution
2.
>> fooEnter an int: 12 //here I've written "12" and clicked enter
//end of execution
3.
>> Enter an int: 12 //here I've written "12" and clicked enter
>> foo
//end of execution

如您所见,有时我必须单击回车键才能看到"foo"字符串。在我看来,它应该每次打印,因为它是在单独的线程中启动的。也许 std::cin 以某种方式阻止了 std::cout?如果是这样,那我该怎么办?

这是

完全正常的,默认情况下,输出到std::cout是缓冲的。 coutcin相关联,因此当您开始从cin读取或按回车键时,cout缓冲区将被刷新并出现在屏幕上。

可能发生的情况是第一个线程写入其输出,该输出被缓冲,然后等待输入,刷新输出缓冲区(因此您会看到"Enter an int:"),然后第二个线程写入其输出,但它位于缓冲区中,直到读取输入,当输出再次刷新时。

您可以通过手动刷新其缓冲区来强制第二个线程立即输出:

 std::cout << "foo" << std::flush;

这可能会导致"fooEnter an int:""Enter an int:foo"但您不需要按 Enter 即可显示"foo"