std::basic_ostream<CharT,Traits>::flush

Writes uncommitted changes to the underlying output sequence. Behaves as an UnformattedOutputFunction.

# Declarations

basic_ostream& flush();

# Return value

*this

# Example

#include <chrono>
#include <iostream>
#include <thread>
 
using namespace std::chrono_literals;
 
void f()
{
    std::cout << "Output from thread... ";
    for (int i{1}; i != 10; ++i)
    {
        std::this_thread::sleep_for(250ms);
        std::cout << i << ' ';
 
        // output three numbers at once;
        // the effect is observable only in real-time
        if (0 == (i % 3))
            std::cout.flush();
    }
    std::cout << std::endl; // flushes as well
}
 
int main()
{
    std::thread tr{f};
    std::this_thread::sleep_for(150ms);
    std::clog << "Output from main\n";
    tr.join();
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 581C++98flush() did not behave as an UnformattedOutputFunctionbecause of the resolution of LWG issue 60behaves as anUnformattedOutputFunction

# See also