多次调用 std::cout 使子进程挂起
Multiple calls to std::cout make subprocess hang
我把我之前问题的一部分复制到这里来描述问题:
I wrote an application in C++ that has two parts - the frontend and
the backend. These two communicate using IPC layer provided by
wxWidgets. In the backend I use some legacy functions for image data
manipulation. One of these functions hangs or falls into some infinite
loop sometimes (I can observe that 0% of the process resources are
used by the process after some point), but this happens only if I ran
the backend as a subprocess of the frontend. Otherwise (when I run it
manually) it works just fine.
事实证明,使用 std::cout
打印了太多行是造成这种情况的原因,但我想了解原因。会不会是 wxWidgets 使用了一些缓冲区来存储应用程序输出并且打印只是溢出了它?或者这是 Windows 的原生问题?或者它可能与 std::cout
实施有关? 我很确定我无法用 printf
重现这个问题,看来我错了 - printf
似乎也触发了这个问题
标准输出缓冲区大小有限。一定有什么东西正在读取您正在写入缓冲区的内容,无论是文件、控制台 window 还是其他进程。如果您的写入速度快于 reader 能够处理的速度,那么缓冲区最终将填满并阻止任何进一步的写入,直到 reader 读取了一些数据。
我把我之前问题的一部分复制到这里来描述问题:
I wrote an application in C++ that has two parts - the frontend and the backend. These two communicate using IPC layer provided by wxWidgets. In the backend I use some legacy functions for image data manipulation. One of these functions hangs or falls into some infinite loop sometimes (I can observe that 0% of the process resources are used by the process after some point), but this happens only if I ran the backend as a subprocess of the frontend. Otherwise (when I run it manually) it works just fine.
事实证明,使用 std::cout
打印了太多行是造成这种情况的原因,但我想了解原因。会不会是 wxWidgets 使用了一些缓冲区来存储应用程序输出并且打印只是溢出了它?或者这是 Windows 的原生问题?或者它可能与 std::cout
实施有关? 我很确定我无法用 重现这个问题,看来我错了 - printf
printf
似乎也触发了这个问题
标准输出缓冲区大小有限。一定有什么东西正在读取您正在写入缓冲区的内容,无论是文件、控制台 window 还是其他进程。如果您的写入速度快于 reader 能够处理的速度,那么缓冲区最终将填满并阻止任何进一步的写入,直到 reader 读取了一些数据。