Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ join()上的Boost线程分段错误_C++_Boost_Boost Thread - Fatal编程技术网

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
作为
公共
函数已经存在很长时间了。。。