Boost logo

Boost :

From: Andreas Huber (ah2003_at_[hidden])
Date: 2004-02-14 04:40:23


Sean Kelly wrote:
[snip]
>
> The user could delete the object passed to boost::thread with the
> existing design and there would be no provision for calling join. If
> this is
> enough of a concern, make the thread_base destructor a pure virtual
> function and document the need to call join.

Yes, but the user still has to know/remember that (s)he has to call join()
in the derived class destructor and start() in the constructor. I think the
boost::thread design is much less error-prone here. Plus, with the
boost::thread design it is easy to avoid lifetime issues altogether:

// untested code
class MyActiveObject
{
  public:
    void Run()
    {
      // ...
    }
};

// ...

boost::thread myThread( boost::function< void () >(
  boost::shared_ptr< MyActiveObject >( new MyActiveObject() ),
  &MyActiveObject::Run ) );

This will work even if you forget to call join() before destructing the
thread object...

Regards,

Andreas


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk