一定の間隔でフラッシュするバッファリングされたストリームを実装するための標準的な方法は?

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

質問

与えられたパケット/秒間隔でパケットを生成するソースからパケットをシミュレートしています。ostreamオブジェクトのように動作するストリームクラスを作成し、operator<<を使用してそれを通してそれらを出力することを許可しますが、挿入された各値がバッファからファイル記述子に解放されるべきであることを指定して、指定された間隔。 たとえば、ファイル記述子sockfdを持つソケットがある場合があります。

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

と出力は出力されるようにタイミングされます。

1
<1 second gap>
2
<1 second gap>
3
. ソケットへの

。私は今Boost.iostreamsを見ています、それは良い解決策になりますか?私が知っていないこの問題を説明している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