Search code examples
c++boostboost-thread

BOOST threading : cout behavior


I am new to Boost threading and I am stuck with how output is performed from multiple threads. I have a simple boost::thread counting down from 9 to 1; the main thread waits and then prints "LiftOff..!!"

#include <iostream>
#include <boost/thread.hpp>
using namespace std;

struct callable {
    void operator() ();
};

void callable::operator() () {
    int i = 10;
    while(--i > 0) {
        cout << "#" << i << ", ";
        boost::this_thread::yield();
    }
    cout.flush();
}

int main() {
    callable x;
    boost::thread myThread(x);

    myThread.join();

    cout << "LiftOff..!!" << endl;

    return 0;
}

The problem is that I have to use an explicit "cout.flush()" statement in my thread to display the output. If I don't use flush(), I only get "LiftOff!!" as the output.

Could someone please advise why I need to use flush() explicitly?


Solution

  • This isn't specifically thread related as cout will buffer usually on a per thread basis and only output when the implementation decides to - so in the thread the output will only appear on a implementation specific basic - by calling flush you are forcing the buffers to be flushed.

    This will vary across implementations - usually though it's after a certain amount of characters or when a new line is sent.

    I've found that multiple threads writing too the same stream or file is mostly OK - providing that the output is performed as atomically as possible. It's not something that I'd recommend in a production environment though as it is too unpredictable.