1

I'd like to use a FIFO buffer from a C++ code. There are two processes, one of them always writes to the FIFO, the other always reads it. It's very simple.

I don't really need to read the contents of the buffer, I just want to know how much data is in it, and clear it.

What is the most sophisticated solution for this in C++?

This code works well, but I never need the buffer's contents:

int num;
char buffer[32];

num = read(FIFO, buffer, sizeof(buffer));
//num is the important variable

Thank you!

4

4 回答 4

0

据我所知,从 linux FIFO 中清除字节(不破坏 FIFO)的唯一方法是将它们读出。您可以通过一次读取大量数据来更快地清除它们(32这是一个非常小的读取大小,除非这是通常写入 FIFO 的大小)。如果您处于阻塞模式,则应按照 Robert Mason 指示的链接中所述查询字节。如果描述符处于非阻塞模式,则可以读取直到EAGAIN返回以知道它已被清除。您可以使用poll来确定更多数据何时到达 FIFO。

于 2012-06-25T00:25:11.087 回答
0

清除管道的唯一方法是读取它,因此存在多少字节的问题没有实际意义——您在阅读它们之后就会知道。真正的问题最终与任何阅读相同:

(1) 如果您不关心数据,那么大概您不想阻塞等待它,因此使 FIFO 非阻塞。

(2) 由于您大概不想坐下来浪费时间轮询 FIFO 以查看是否有要读取的内容,因此您应该将 FIFO fd 放入select语句中。当有要阅读的内容时,将其排空并添加到计数器中。

于 2012-06-25T00:55:33.947 回答
0

你可以看看这个问题:Determing the number of bytes ready to be recv()'d

在 linux 上,套接字的代码也应该在 FIFO 上以最小的努力工作。不过,我不确定 Windows。

于 2012-06-24T22:24:44.493 回答
-1

Not sure if I've got you right, sophisticated - do you mean the most efficient, or the most obfuscated?

Anyway, if you don't need the buffer contents - you may just use a (shared) interlocked variable.

于 2012-06-24T22:05:56.633 回答