如何判断管道上是否有新数据
How can I tell if there is new data on a pipe?
我在Windows上工作,我正在努力学习管道以及它们是如何工作的。
我还没有发现的一件事是,我如何判断管道上是否有新数据(来自管道的子/接收器端?
通常的方法是有一个线程来读取数据,并发送数据进行处理:
void GetDataThread()
{
while(notDone)
{
BOOL result = ReadFile (pipe_handle, buffer, buffer_size, &bytes_read, NULL);
if (result) DoSomethingWithTheData(buffer, bytes_read);
else Fail();
}
}
问题是ReadFile()函数等待数据,然后读取它。有没有一种方法可以在不等待新数据的情况下判断是否有新数据,比如
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();
}
}
使用PeekNamedPipe()
:
DWORD total_available_bytes;
if (FALSE == PeekNamedPipe(pipe_handle,
0,
0,
0,
&total_available_bytes,
0))
{
// Handle failure.
}
else if (total_available_bytes > 0)
{
// Read data from pipe ...
}
还有一种方法是使用IPC同步原语,如事件(CreateEvent())。在使用复杂逻辑进行进程间通信的情况下,您也应该关注它们。
相关文章:
- 如何在新的派生对象中获取基本对象的数据?
- C++ TCP 套接字通信 - 连接按预期工作,几秒钟后失败,没有收到新数据,read() 和 recv() 块
- 如果我们不创建一个新节点并使用指针插入数据并建立链接(在链表中)怎么办?
- 将数据复制到磁盘上新位置的语法
- 如何使用 boost::asio 的 async_read_some() 读取所有可用数据,而无需等待新数据到达?
- 将分配的内存与基本数据类型一起使用时,是否需要新放置? std::complex?
- 使用在R中训练的神经网络来预测C 中的新数据
- 将元音与字符串数据类型的字符串分开,并将其存储在新字符串中
- cpp 中是否存在一种数据结构,可以轻松地提供一种基于已存在的实例构建新结构的方法
- 如何创建一个C ++函数,该函数在每次用户输入新数据时创建新对象
- 我的数组大小调整代码在新数据上引发异常[size_t]
- 我如何在参考上使用数据成员而不使用新关键字创建对象
- 如何将字符**中的数据值复制到新字符**中
- 导致ESP32随机停止并触发看门狗计时器重新启动的串行数据包
- C++ 在覆盖新运算符和删除运算符时不释放数据
- C++,打开和编辑文本文件中的数字数据,并将结果放入新文件中
- 新任务中以前的数据会发生什么变化
- 插入功能指向新数据
- 如何实现消费者生产者,消费者可以请求新的数据
- 在 C++ 中创建值为 0 到 63 的新"int64"数据类型