C++ join()上的Boost线程分段错误
我有以下代码:C++ join()上的Boost线程分段错误,c++,boost,boost-thread,C++,Boost,Boost Thread,我有以下代码: #include <cstdio> #include <boost/thread.hpp> void foo() { puts("foo()"); } int main() { boost::thread t(foo); //t.start_thread(); puts("join()"); t.join(); return 0; } #包括 #包括 void foo(){ 看跌期权(“foo()”);
#include <cstdio>
#include <boost/thread.hpp>
void foo() {
puts("foo()");
}
int main() {
boost::thread t(foo);
//t.start_thread();
puts("join()");
t.join();
return 0;
}
#包括
#包括
void foo(){
看跌期权(“foo()”);
}
int main(){
boost::线程t(foo);
//t、 启动_线程();
put(“join()”);
t、 join();
返回0;
}
它工作正常,但当我取消注释start\u thread()
调用时,它会在join()
中崩溃
为什么start\u thread()
调用会导致join()
中出现分段错误?
我使用:
g++(Ubuntu 4.8.2-19ubuntu1)4.8.2
Boost版本:1.54.0.1ubuntu1
g++-std=c++11-static main.cpp-lboost_thread-lboost_system-lpthread-L/usr/lib/x86_64-linux-gnu/
Boost线程在Boost::thread
的构造函数中执行,没有必要也不应该显式启动它。实际上,boost::thread
的ctor调用了start\u thread()
,start\u thread()
调用了start\u thread\u noexcept()
,实现了在不同平台上创建线程,它调用了pthread\u create()
,如果您使用pthread
,您可以从boost thread的源代码中检查这一点。我想知道为什么这个函数是公共的。更新:刚刚签出了boost(1.57)的新版本,该函数现在在文件
boost/thread/detail/thread.hpp中声明为私有:
private:
bool start_thread_noexcept();
bool start_thread_noexcept(const attributes& attr);
//public:
void start_thread()
{
if (!start_thread_noexcept())
{
boost::throw_exception(thread_resource_error());
}
}
void start_thread(const attributes& attr)
{
if (!start_thread_noexcept(attr))
{
boost::throw_exception(thread_resource_error());
}
}
因此,如果您想调用它,它将无法编译 SIGSEGV并不是人们通常期望从如此成熟和广泛使用的库中得到的。我也对公共start\u thread()
函数感到困惑。当我看到start\u-thread()
是公共的时,我假设API要求您调用start\u-thread()
-->导致我的代码不能很好地退出,然后调试器无法向我显示它是如何失败的。我终于注意到2倍的打印输出,这使我重新阅读了文档,并重新实现了我的代码,使其不调用start\u-thread()
。我有1.54个boost,所以start\u-thread
作为公共函数已经存在很长时间了。。。