我正在运行2个线程,并且在执行线程
之后显示我首先显示的文本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();
}
在主程序中我刚刚显示了一个文本asd
,它总是在执行线程后显示..
答案 0 :(得分:3)
由于缓存了cout
,因此放在它上面的数据可能不会立即出现在控制台上(或者可能被重定向到的位置)。因此,尝试在线程内刷新输出流。 E.g。
cout << "asd" << endl;
答案 1 :(得分:3)
std::cout << "asd" << std::flush;
答案 2 :(得分:0)
我无法评论你的原帖(还没有足够的帖子);在切线上注意,考虑使用scoped_lock(如果你还没有!),比显式锁定/解锁调用更安全......
还要提醒一句,冲洗是昂贵的,只在必要时才打电话。