开发者

How read from multiple pipes in Windows in same thread

In my program I have saveral pipes which are connected to stdout and stderr streams of child processes (i.e. in main pr开发者_JS百科ocess 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜