简体   繁体   English

多次调用std :: cout使子进程挂起

[英]Multiple calls to std::cout make subprocess hang

I'll copy here part of my previous question to describe the problem: 我将在这里复制上一个问题,以描述问题:

I wrote an application in C++ that has two parts - the frontend and the backend. 我用C ++编写了一个包含两部分的应用程序-前端和后端。 These two communicate using IPC layer provided by wxWidgets. 这两个使用wxWidgets提供的IPC层进行通信。 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. 这些功能之一有时会挂起或陷入无限循环(我可以观察到某个时间点后,进程使用了​​0%的进程资源),但这仅在将后端作为前端的子进程运行时才会发生。 Otherwise (when I run it manually) it works just fine. 否则(当我手动运行它时)就可以正常工作。

It turns out that printing too many lines with std::cout was causing that, but I'd like to understand why. 事实证明,使用std::cout打印太多行会导致这种情况,但我想了解原因。 Could it be that wxWidgets utilizes some buffer for storing application output and printing was simply overflowing it? 可能是wxWidgets使用了一些缓冲区来存储应用程序输出,而打印只是使它溢出了吗? Or this is rather native issue of Windows? 还是这是Windows的固有问题? Or maybe it could be related to std::cout implementation? 也许它可能与std::cout实现有关? I'm pretty sure I'm not able to reproduce this with printf It seems that I was wrong - printf also seems to trigger that issue 我很确定我无法用printf重现它似乎我错了printf也似乎触发了该问题

The stdout buffer is of a finite size. 标准输出缓冲区的大小是有限的。 Something must be reading what you are writing into the buffer, whether this is a file, a console window or another process. 无论是文件,控制台窗口还是其他进程,都必须正在读取要写入缓冲区的内容。 If you write faster than the reader is able to cope with then the buffer will eventually fill up and block any further writes until the reader has read some data. 如果您写入的速度快于读取器无法应付的速度,则缓冲区最终将填满并阻止任何进一步的写入,直到读取器读取了一些数据。

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

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