Boost logo

Boost Users :

From: Sohail Somani (sohail_at_[hidden])
Date: 2008-01-26 19:50:55


On Sun, 27 Jan 2008 10:54:27 +1100, anthony kelly wrote:

> The only examples I can find show starting the thread at the same time
> it is defined! Is it possible to define the thread as a class member,
> and start it later? If so, what would the syntax for this line be? If
> not, can you suggest an alternative structure?

Hi,

That is the only way I've used it but you can always use a shared_ptr to
the thread:

class C
{...
  private:
 ...
   boost::shared_ptr<boost::thread> thread;
};

void C::Start()
{
   running=true;
   thread.reset(new thread(&C::ThreadJob));
}

Only thing you'd have to be careful of is copying of class C but you can
inherit from boost::noncopyable to avoid any issues.

-- 
Sohail Somani
http://uint32t.blogspot.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net