简体   繁体   English

冲洗缓冲区意味着什么?

[英]What does flushing the buffer mean?

I am learning C++ and I found something that I can't understand: 我正在学习C ++,我找到了一些我无法理解的东西:

Output buffers can be explicitly flushed to force the buffer to be written. 可以显式刷新输出缓冲区以强制写入缓冲区。 By default, reading cin flushes cout ; 默认情况下,阅读cin刷新cout ; cout is also flushed when the program ends normally. 当程序正常结束时, cout也会被刷新。

So flushing the buffer (for example an output buffer): does this clear the buffer by deleting everything in it or does it clear the buffer by outputting everything in it? 因此刷新缓冲区(例如输出缓冲区):这是通过删除缓冲区中的所有内容来清除缓冲区还是通过输出缓冲区中的所有内容来清除缓冲区? Or does flushing the buffer mean something completely different? 或者冲洗缓冲区意味着完全不同的东西?

Consider writing to a file. 考虑写入文件。 This is an expensive operation. 这是一项昂贵的操作。 If in your code you write one byte at a time, then each write of a byte is going to be very costly. 如果在代码中一次写入一个字节,那么每次写入一个字节将会非常昂贵。 So a common way to improve performance is to store the data that you are writing in a temporary buffer. 因此,提高性能的常用方法是将要写入的数据存储在临时缓冲区中。 Only when there is a lot of data is the buffer written to the file. 只有当有大量数据时,缓冲区才会写入文件。 By postponing the writes, and writing a large block in one go, performance is improved. 通过推迟写入和一次写入大块,性能得到改善。

With this in mind, flushing the buffer is the act of transferring the data from the buffer to the file. 考虑到这一点,刷新缓冲区是将数据从缓冲区传输到文件的行为。

Does this clear the buffer by deleting everything in it or does it clear the buffer by outputting everything in it? 这是通过删除缓冲区中的所有内容来清除缓冲区还是通过输出缓冲区中的所有内容来清除缓冲区?

The latter. 后者。

You've quoted the answer: 你引用了答案:

Output buffers can be explicitly flushed to force the buffer to be written. 可以显式刷新输出缓冲区以强制写入缓冲区。

That is, you may need to "flush" the output to cause it to be written to the underlying stream (which may be a file, or in the examples listed, a terminal). 也就是说,您可能需要“刷新”输出以使其被写入底层流(可能是文件,或者在列出的示例中,即终端)。

Generally, stdout/cout is line-buffered: the output doesn't get sent to the OS until you write a newline or explicitly flush the buffer. 通常,stdout / cout是行缓冲的:在您编写换行符或显式刷新缓冲区之前,输出不会发送到操作系统。 The advantage is that something like std::cout << "Mouse moved (" << px << ", " << py << ")" << endl causes only one write to the underlying "file" instead of six, which is much better for performance. 优点是像std::cout << "Mouse moved (" << px << ", " << py << ")" << endl只导致一次写入底层“文件”而不是六次,这对性能要好得多。 The disadvantage is that a code like: 缺点是代码如:

for (int i = 0; i < 5; i++) {
    std::cout << ".";
    sleep(1); // or something similar
}

std::cout << "\n";

will output ..... at once (for exact sleep implementation, see this question ). 将立即输出..... (对于确切的sleep实现,请参阅此问题 )。 In such cases, you will want an additional << std::flush to ensure that the output gets displayed. 在这种情况下,您需要额外的<< std::flush以确保显示输出。

Reading cin flushes cout so you don't need an explicit flush to do this: 阅读cin刷新cout所以你不需要显式刷新来执行此操作:

std::string colour;
std::cout << "Enter your favourite colour: ";
std::cin >> colour;

通过输出所有内容来清除缓冲区。

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

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