线程库的奇怪行为
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
是缓冲的。 cout
与cin
相关联,因此当您开始从cin
读取或按回车键时,cout
缓冲区将被刷新并出现在屏幕上。
可能发生的情况是第一个线程写入其输出,该输出被缓冲,然后等待输入,刷新输出缓冲区(因此您会看到"Enter an int:"
),然后第二个线程写入其输出,但它位于缓冲区中,直到读取输入,当输出再次刷新时。
您可以通过手动刷新其缓冲区来强制第二个线程立即输出:
std::cout << "foo" << std::flush;
这可能会导致"fooEnter an int:"
或"Enter an int:foo"
但您不需要按 Enter 即可显示"foo"
。
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- C++使用params创建线程函数会导致转换错误
- 类与私有变量的其他类之间的线程安全性
- CoInitialize()在单独的线程上崩溃而不返回
- c++中的线程池
- 线程之间的布尔停止信号
- 为什么std::async使用同一个线程运行函数
- 用于矢量处理的多个线程
- C++为线程工作动态地分割例程
- 为什么我不能在 while 循环中创建线程?