Boost::Thread constructor
Hello Boost Users, I would like to implement a simple threaded class like this: class C { public: void Start(); void Stop(); private: bool running; boost::thread t; void ThreadJob(); } void C::Start() { running = true; t = boost::thread(&C::ThreadJob); } void C::Stop() { running = false; t.join(); } void C::ThreadJob() { while(running); } However, the syntax for this line is wrong: t = boost::thread(&C::ThreadJob); 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? Thank you, Anthony
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_ptrboost::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
On 26/01/2008, Sohail Somani
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.
Using auto_ptr instead of shared_ptr would be another option, if you prefer ownership transfer semantics.
thread.reset(new thread(&C::ThreadJob));
Note also that a member function pointer is not usable on its own, so you'll need to do something like this instead: thread.reset(new thread( bind(&C::ThreadJob, this) ));
On Sat, 26 Jan 2008 19:59:42 -0500, Scott McMurray wrote:
On 26/01/2008, Sohail Somani
wrote: 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.
Using auto_ptr instead of shared_ptr would be another option, if you prefer ownership transfer semantics.
Either way, auto_ptr is the right choice. Thanks.
Note also that a member function pointer is not usable on its own, so you'll need to do something like this instead:
thread.reset(new thread( bind(&C::ThreadJob, this) ));
You caught me blindly copying! -- Sohail Somani http://uint32t.blogspot.com
Thank you
On Jan 27, 2008 12:29 PM, Sohail Somani
On Sat, 26 Jan 2008 19:59:42 -0500, Scott McMurray wrote:
On 26/01/2008, Sohail Somani
wrote: 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.
Using auto_ptr instead of shared_ptr would be another option, if you prefer ownership transfer semantics.
Either way, auto_ptr is the right choice. Thanks.
Note also that a member function pointer is not usable on its own, so you'll need to do something like this instead:
thread.reset(new thread( bind(&C::ThreadJob, this) ));
You caught me blindly copying!
-- Sohail Somani http://uint32t.blogspot.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
anthony kelly
-
Scott McMurray
-
Sohail Somani