如何判断管道上是否有新数据? [英] How can I tell if there is new data on a pipe?
本文介绍了如何判断管道上是否有新数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Windows,并且正在尝试学习管道及其工作方式.
I'm working on Windows, and I'm trying to learn pipes, and how they work.
我还没有发现的一件事是如何判断管道上是否有新数据(从管道的子/接收器端开始?
One thing I haven't found is how can I tell if there is new data on a pipe (from the child/receiver end of the pipe?
通常的方法是让一个线程读取数据,并将其发送以进行处理:
The usual method is to have a thread which reads the data, and sends it to be processed:
void GetDataThread()
{
while(notDone)
{
BOOL result = ReadFile (pipe_handle, buffer, buffer_size, &bytes_read, NULL);
if (result) DoSomethingWithTheData(buffer, bytes_read);
else Fail();
}
}
问题在于ReadFile()函数等待数据,然后读取它.有没有一种方法可以在不真正等待新数据的情况下判断是否有新数据,就像这样:
The problem is that the ReadFile() function waits for data, and then it reads it. Is there a method of telling if there is new data, without actually waiting for new data, like this:
void GetDataThread()
{
while(notDone)
{
BOOL result = IsThereNewData (pipe_handle);
if (result) {
result = ReadFile (pipe_handle, buffer, buffer_size, &bytes_read, NULL);
if (result) DoSomethingWithTheData(buffer, bytes_read);
else Fail();
}
DoSomethingInterestingInsteadOfHangingTheThreadSinceWeHaveLimitedNumberOfThreads();
}
}
推荐答案
使用 查看全文