简体   繁体   English

boost ::线程分段错误

[英]boost::thread segmentation fault

I have Runnable class. 我有Runnable课。 In which, following functions I use to start new thread: 在其中,我使用以下函数来启动新线程:

      start()
      {
           status_ = RUNNING;
            mythread_ = boost::thread(boost::ref(*this)); // Line 2
      }

I have Controller class derived from Runnable. 我有从Runnable派生的Controller类。 and I want to create thread for Controller using start() function 我想使用start()函数为Controller创建线程

So, In Controller start function, 因此,在控制器启动功能中,

I use: 我用:

controller_->start()

to create a new thread; 创建一个新线程;

But which eventually leads to segmentation fault at Line 2. 但这最终导致2号线出现分段错误。

Any idea what could have been gone wrong? 知道发生了什么错误吗?

I remember sometimes not specifying the thread library to the compiler has resulted in segfault. 我记得有时不向编译器指定线程库会导致segfault。 Try adding -pthread argument to the compiler if you're using unix. 如果使用的是UNIX,请尝试将-pthread参数添加到编译器。 It seems though that it's not needed on latest linux/boost/gcc anymore. 似乎在最新的linux / boost / gcc中不再需要它了。

The object's address is only available from within the member function as the this pointer and most uses of this are implicit. 该对象的地址只能从成员函数作为内this指针的大多数使用this是隐含的。

Alternatively, you can make the start() function, friend of the class and directly sending reference of the object to your new thread. 或者,您可以使start()函数成为类的friend ,并将对象的引用直接发送到新线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM