Стандартный способ реализации буферизованного потока, который промывается с постоянным интервалом?

StackOverflow https://stackoverflow.com/questions/5034202

Вопрос

Я моделирую пакеты из источника, который производит пакеты в данном пакете / втором интервале.Я хочу сделать класс Stream, который работает как объект ostream, что позволяет использовать operator<< для вывода вещей через него, но с предупреждением, что каждое значение вставленное значение должно быть выпущено из буфера к дескриптору файла, в порядке, в указанноминтервал.

Так, например, у меня может быть разъем с файловым дескриптором GeneracodicCode и скажем:

MyBuffer buffer(sockfd, 1000); //Interval of 1000 milliseconds
buffer << 1 << 2 << 3;
.

и вывод будет приурочен так, что он выводится

1
<1 second gap>
2
<1 second gap>
3
.

к розетке.Я смотрю на Boost.oidreams прямо сейчас, это будет хорошее решение?Есть ли волшебная фраза, которую я могу Google, которая описывает эту проблему, что я не знаю?

Любая помощь будет оценена.

спасибо Брэд

Это было полезно?

Решение

One option for doing this that's completely orthogonal to building a custom streams class would be to maintain a queue of strings that's polled by a thread every second. Each time the queue is polled, the thread reads out the first element and sends it across the network.

This doesn't use the streams library, but I think that might be what you want. Internally, most streams just glob together all the input they get into a mass of text, losing the information about which parts of the text correspond to each object you inserted.

EDIT: I should have mentioned this the first time around, but please be sure to use the appropriate synchronization on this queue! You'll probably want to use a mutex to guard access to it, or to use a clever lock-free queue if that doesn't work. Just be sure not to blindly read and write to it from multiple threads.

Другие советы

Should the 1000ms be asynchronous ? If not, you could put a Sleep(1000) in your stream's operator<<. Depending on what you're trying to do, it could suit you.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top