如何在同一线程中从Windows中的多个管道读取

发布于 2024-10-22 05:59:25 字数 218 浏览 3 评论 0原文

在我的程序中,我有连接到子进程的 stdout 和 stderr 流的保存管道(即在主进程中我正在从该流中读取)。但是,当其中之一没有任何内容可供读取时,我的程序就会挂起。有没有办法不使用线程来解决这个问题。另外,如果在 x 毫秒内没有任何内容可读取,我还希望终止所有子进程。

在unix中 select() + non_blocking read 解决了这两个问题。但是窗户呢?

In my program I have saveral pipes which are connected to stdout and stderr streams of child processes (i.e. in main process I'm reading from this streams). But when there is nothing to read from one of them my program hangs. Is there way to solve this problem not using thread. Also I want all child processes to be killed if there is nothing to read during x msecs.

In unix select() + non_blocking read solves this two problems. But what about windows?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何止钟意 2024-10-29 05:59:25

您可以在 Windows 中使用类似的方法。使用 OVERLAPPED 结构,您可以针对管道发出异步 I/O。然后在关联的事件句柄上使用 WaitForMultipleObjects 并设置超时(这是选择模拟)。有关选项的概述,请参阅

You can use a similar approach in windows. Using OVERLAPPED structs, you can issue asynchronous I/O against the pipes. Then use WaitForMultipleObjects on the associated event handles with a timeout (this is the select analog). See this for an overview of the options.

渔村楼浪 2024-10-29 05:59:25

如果“挂起”意味着您的 GUI 应用程序停止响应,我认为您正在寻找 MsgWaitForMultipleObjects 将使您的 GUI 泵送消息,同时等待最多 31 个句柄发出信号。

将其与 OVERLAPPED IO 结合起来,实际上将读取完成事件转换为可等待信号(您不能直接将文件句柄传递给任何 WaitForXXX 函数)。

如果您需要同时等待超过 30 个读取事件,那么您将需要使用工作线程,可能还需要使用 IO 完成端口。

If by "hangs" you mean your GUI app stops responding, I think you are looking for MsgWaitForMultipleObjects that will let your GUI pump messages, while waiting for up to 31 handles to be signalled.

Combine it with OVERLAPPED IO to actually turn the read completion events into waitable signals (You can't directly pass a file handle to any of the WaitForXXX functions).

If you need to wait for more than ~30 read events at one time then you will need to use worker threads, and possibly IO Completion Ports.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文