呼叫叉时缓冲C 流的种族条件 - 力冲洗
Race condition of buffered C++ streams when calling fork -- force flush?
我有一个程序,该程序用unix fork()
产生了作者线程。这可以正常工作,但是当尚未刷新C 流时,我会得到一个竞赛条件,其中两个线程输出相同的数据。以下示例显示了我的意思:
extern "C" {
#include <sys/stat.h>
#include <unistd.h>
#include <sys/wait.h>
}
#define W 10
#include <iostream>
int main(void)
{
pid_t pid;
int status;
for (int i = 0; i < (1 << W); i++) {
// spawn a child after adding to the std::cout buffer
if (i == (1 << (W - 1))) {
// std::cout.flush(); // (1)
pid = fork();
if (!pid)
break;
}
// join the child thread after a while
if (i == 3 * (1 << (W - 2)))
waitpid(pid, &status, 0);
// print stuff to the stream
std::cout << i << 'n';
// std::cout << i << std::endl; // (2)
}
return EXIT_SUCCESS;
}
因此,我尝试过的解决方法是(1)在调用fork()
(首选解决方案)之前先手动冲洗std::cout
,或(2)在写入流时使用std::endl
,但这添加了不必要的flush
调用。尽管这种适用于全球可访问的std::cout
的作品,但我的首选解决方案(1)对其他可访问全球访问的缓冲流不起作用。此外,在某个时候,我可能会打开另一个文件,然后我可能会忘记冲洗。
有更好的解决方案解决这个问题吗?像潮红的函数一样 编辑 建议的解决方案是使用C库中的 在我的系统上我得到 不必说线的数量应该相等。 有其他想法?fflush(nullptr)
冲洗所有(C)流。这适用于与stdout
和stderr
同步的std::cout
和std::cerr
,但是其他C 缓冲流将不同步。这证明了问题:extern "C" {
#include <sys/stat.h>
#include <unistd.h>
#include <sys/wait.h>
}
#include <iostream>
#include <fstream>
#define W 10
int main(void)
{
pid_t pid;
int status;
std::ofstream fout("foo");
for (int i = 0; i < (1 << W); i++) {
if (i == (1 << (W - 1))) {
fflush(nullptr); // this works for std::{cout,cerr} but not files
pid = fork();
if (!pid)
return EXIT_SUCCESS;
}
if (i == 3 * (1 << (W - 2)))
waitpid(pid, &status, 0);
fout << i << 'n';
std::cout << i << 'n';
}
fout.close();
return EXIT_SUCCESS;
}
$ ./a.out 1>bar; wc -l foo bar
1536 foo
1024 bar
使用 fflush
,然后传递 nullptr
。
来自男人:
#include <cstdio> // adapted the include for C++
int fflush(FILE *stream);
如果流参数为null,则fflush()冲洗所有打开的输出流。
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 在没有太多条件句的情况下,我如何避免被零除
- 基于多个条件处理地图中的所有元素
- 条件constexpr函数
- 无论条件是否为true,if总是在c++中执行
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 基于模板值的条件变量
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 将按位if条件转换为普通if条件
- 条件断点在不应该触发时触发
- 如何使用原子指针执行双缓冲
- 为什么简单的算术减法在"if"条件下不起作用?
- 如何在for循环中包含两个索引值的测试条件
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 我提出什么条件才能再加5%的折扣
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- 即使没有满足他们的条件,我也无法通过一些 do-while 循环
- 如何编写一个使用n倍三元条件语句的C++布尔函数
- 呼叫叉时缓冲C 流的种族条件 - 力冲洗
- c++有效的缓冲文本和有条件地将部分写入文本文件的方法