简体   繁体   English

std :: iostream是否是非阻塞的?

[英]Is std::iostream non-blocking?

According to the boost reference for Boost.Iostreams (In section 3.6, at the very bottom): 根据Boost.Iostreams的提升参考(在第3.6节,在最底部):

http://www.boost.org/doc/libs/1_64_0/libs/iostreams/doc/index.html http://www.boost.org/doc/libs/1_64_0/libs/iostreams/doc/index.html

Although the Boost.Iostreams Filter and Device concepts can accommodate non-blocking i/o, the C++ standard library stream and stream buffer interfaces cannot, since they lack a means to distinguish between temporary and permanent failures to satisfy a read or write request 虽然Boost.Iostreams过滤器和设备概念可以适应非阻塞i / o,但C ++标准库流和流缓冲接口不能,因为它们缺乏区分临时和永久失败以满足读或写请求的方法

However, the function std::istream::readsome appears to be non-blocking, in that the available characters will be immediately returned, without a blocking (except for a RAM copy) wait. 但是,函数std::istream::readsome似乎是非阻塞的,因为可以立即返回可用字符,而不会阻塞(RAM副本除外)等待。 My understanding is that: 我的理解是:

std::istream::read will block until eof or number of characters read. std::istream::read将阻塞,直到std::istream::read eof或字符数。

std::istream::readsome will return immediately with characters copied from the internal buffer. std::istream::readsome将立即返回从内部缓冲区复制的字符。

I agree with you that readsome is not a blocking operation. 我同意你的说法, readsome不是阻塞操作。 However, as specified, it is wholly inadequate as an interface for performing what is usually called "non-blocking I/O". 然而,如所指定的,作为用于执行通常称为“非阻塞I / O”的接口,它完全不合适。

First, there is no guarantee that readsome will ever return new data, even if it is available. 首先,有没有保证, readsome回到新的数据,即使它是可用的。 So to guarantee you actually make progress, you must use one of the blocking interfaces eventually. 因此,为了确保您实际取得进展,您必须最终使用其中一个阻止接口。

Second, there is no way to know when readsome will return data. 其次,没有办法知道readsome什么时候会返回数据。 There is no way to "poll" the stream, or to get a "notification" or "event" or "callback". 无法“轮询”流,或获取“通知”或“事件”或“回调”。 A usable non-blocking interface needs at least one of these. 可用的非阻塞接口至少需要其中一个。

In short, readsome appears to be a half-baked and under-specified attempt to provide a non-blocking interface to I/O streams. 简而言之, readsome似乎是一种readsome指定的尝试,为I / O流提供非阻塞接口。 But I have never seen it used in production code, and I would not expect to. 但我从未见过它在生产代码中使用过,我不希望这样。

I think the Boost documentation overstates the argument, because as you observe, readsome is certainly capable of distinguishing temporary from permanent failure. 我认为Boost文档夸大了论证,因为正如你所观察到的, readsome肯定能够区分暂时性和永久性失败。 But their conclusion is still correct for the reasons above. 但由于上述原因,他们的结论仍然是正确的。

When looking into non-blocking portability, I didn't find anything in the C++ standard library that looked like it did what you think it does. 在研究非阻塞可移植性时,我没有在C ++标准库中找到任何看起来像你认为的那样的东西。

If your goal is portability, my interpretation was that the section that mattered most was this: 如果您的目标是可移植性,我的解释是,最重要的部分是:

http://en.cppreference.com/w/cpp/io/basic_istream/readsome http://en.cppreference.com/w/cpp/io/basic_istream/readsome

For example, when used with std::ifstream, some library implementations fill the underlying filebuf with data as soon as the file is opened (and readsome() on such implementations reads data, potentially, but not necessarily, the entire file), while other implementations only read from file when an actual input operation is requested (and readsome() issued after file opening never extracts any characters). 例如,当与std :: ifstream一起使用时,一旦文件被打开,一些库实现就会用数据填充底层的filebuf(并且这些实现上的readsome()读取数据,可能但不一定是整个文件),而其他实现仅在请求实际输入操作时从文件读取(并且在文件打开后发出的readsome()从不提取任何字符)。

This says that different implementations that use the iostream interface are allowed to do their work lazily, and readsome() doesn't guarantee that the work even gets kicked off. 这表示使用iostream接口的不同实现允许懒惰地执行其工作,并且readsome()不保证工作甚至被启动。

However, I think your interpretation that readsome is guaranteed not to block is true. 但是,我认为您对readome的解释保证不会阻止是真的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM