// Copyright (C) 2001-2003
// William E. Kempf
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/thread/thread.hpp>
#include <iostream>
using namespace std;
void helloworld()
{
std::cout << "Hello World!" << std::endl;
}
int main()
{
boost::thread thrd(&helloworld);
thrd.join();
std::cout<<"sdfsfsd"<<endl;
}
根据教程和许多其他示例,我遇到了以下错误。
我在 OS X 10.8.2 上使用 xcode 4.6.2,安装了带 sudo 端口的 boost 1.54,链接了所有 header 搜索路径和库,并添加了动态库。有人能告诉我这是什么问题吗?如果没有连接,它有时会与文本“hello world”和“sdfsfsd”穿插在一起,因为它不等待线程完成但有时也会给我这个错误。谢谢!
最佳答案
线程必须在程序终止前加入(或分离,IIRC)
来自docs :
Destructor V2
When the boost::thread object that represents a thread of execution is destroyed the program terminates if the thread is joinable.
关于c++ - boost 线程 pthread_mutex_lock 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22120452/