tags:

views:

77

answers:

3

hello , i am running 2 threads and the text i display first is displayed after the execution of thread

string thread(string url)
{
    mutex.lock();
    //some function goes here 
    mutex.unlock();
}

int main()
{
    cout<<"asd";

    boost::thread t1(boost::bind(&thread)); 
    boost::thread t2(boost::bind(&thread));

    t1.join();
    t2.join();
}

in the main program i have just displayed an text asd this displayed always after the execution of the thread ..

+3  A: 

Since cout is buffered, data put to it may not appear immediately on the console (or wherever it may be redirected to). Thus, try flushing the output stream within the thread. E.g.

cout << "asd" << endl;
Péter Török
+3  A: 
std::cout << "asd" << std::flush;
sbi
A: 

I can't comment on your original post (not enough posts yet); on a tangential note, consider using scoped_lock (if you're not already!), safer than explicit lock/unlock calls...

also one word of caution, flush is expensive, call only when necessary.

Nim