开发者

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


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


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;


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜