Question

Today I want to use a boost::scoped_ptr to point to a boost::thread.

In my Thread.h I have boost::scoped_ptr<boost::thread> m_thread and in my Thread.cpp there's a function create() in which the creation of the boost::thread should take place. I tried Thread::m_thread (new boost::thread(attr, boost::bind(%Thread::run, this))); but unsurprisingly it didn't work.

I can't figure out myself (or by using the boost documentation) how I would do this, since I don't fully understand what's happening with the scoped_ptr and how it works. Before I used to use a raw pointer, which worked fine but I'm not allowed to use it at this point.

Thanks for your time!

Was it helpful?

Solution

I don't know what kind of error your got, try this:

class Thread {
 public:
  Thread() : thread_(new boost::thread(boost::bind(&Thread::run, this))) {
  }

  void run() {
  }

  ~Thread() {
    thread_->join();
  }

 private:
  boost::scoped_ptr<boost::thread> thread_;
};

int main() {
  Thread thread;
}

But do not forget, that thread may start before constructor end his job.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top