开发者

How can you disable the buffer in a boost::iostreams sink?

I've written a 'sink' using boost::iostreams, so that I can 开发者_如何转开发essentially have my own code run when someone tries to write to an iostream object.

Unfortunately there is a buffer somewhere in the system, so that my Sink's write() function only gets called every 4kB or so. This is a problem because the sink I am implementing is a fixed size (e.g. 128 bytes) so once this much data has been written it needs to return an error to the caller (something like 'disk full'.)

Because of the buffer the caller is able to write a few kilobytes of data and no errors are returned, then later when the buffer is flushed the extra data is silently lost, which is causing me problems.

Does anyone know if it's possible to somehow disable this buffer? I've tried many of the suggestions here for generic iostreams (e.g. pubsetbuf) but none of them seem to apply to the Boost implementation.

The problem code is part of a library I am working on, which is available from GitHub if it helps (run 'make check' and you will see one failure because of this problem.)

Any suggestions would be greatly appreciated!


I think I've found a workaround. You have to manually decide when you want to perform a flush, but if you do a seek - even seeking to the same position, i.e. stream.seekp(0, std::ios::cur) - then it will cause everything to be flushed as expected.

It's a bit of a horrible workaround but it seems to do the job most of the time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜