##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::basic_ostream::flush

From cppreference.com
< cpp‎ | io‎ | basic ostream

basic_ostream& flush(); |  |

Writes uncommitted changes to the underlying output sequence.

If rdbuf() is a null pointer, does nothing

Otherwise, behaves as an `UnformattedOutputFunction` (since C++11). After constructing and checking the sentry object, calls rdbuf()->pubsync(). If the call returns -1, calls setstate(badbit).

### [edit] Parameters

(none)

### [edit] Return value

*this

### [edit] Exceptions

May throw std::ios_base::failure if exceptions()&badbit!=0.

### [edit] Example

Run this code

    #include <thread>
    #include <iostream>
    #include <chrono>
    void f()
    {
        std::cout << "Output from thread...";
        std::this_thread::sleep_for(std::chrono::seconds(2));
        std::cout << "...thread calls flush()\n";
        std::cout.flush();
    }

    int main()
    {
        std::thread t1(f);
        std::this_thread::sleep_for(std::chrono::seconds(1));
        std::clog << "Output from main\n";
        t1.join();
    }

Output:

    Output from main
    Output from thread.....thread calls flush()

### [edit] See also

sync | synchronizes with the underlying storage device (public member function of `std::basic_istream`) [edit]
flush | flushes the output stream (function template)
endl | outputs '\n' and flushes the output stream (function template)

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_ostream/flush&oldid=66233"