raja
raja

Reputation: 109

problem in threading c++

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 ..

Upvotes: 2

Views: 111

Answers (3)

Nim
Nim

Reputation: 33655

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.

Upvotes: 0

sbi
sbi

Reputation: 223999

std::cout << "asd" << std::flush;

Upvotes: 3

P&#233;ter T&#246;r&#246;k
P&#233;ter T&#246;r&#246;k

Reputation: 116246

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;

Upvotes: 3

Related Questions