Boost logo

Boost :

From: Bill Klein (bill_at_[hidden])
Date: 2001-06-27 02:05:45


[About automatica detachment...]

"Greg Colvin" <gcolvin_at_[hidden]> wrote:
> So to do what I want I have to write
>
> thread* temp = new thread(handler);
> temp.detach();
> delete temp;
>
> whereas with native code I would just do
>
> CreateThread(0,0,handler,0,0,0);
>
> or in java
>
> new Thread(handler);

Actually if we went with the reference counted thread descriptor
(my favourite name being thread_ptr that someone suggested), the
following would do the trick:

  boost::create_thread( handler );

Because this function would return the thread_ptr, whose destructor
would detach() (this makes perfect sense: since the last reference
we had to the thread is gone, we can never access it anyway, so
detach the thread and let it do its own thing). Alternatively you
could have code such as:

  boost::thread_ptr thread = boost::create_thread( handler );
  ...
  ...
  thread->join();

Using the reference counted smart pointer would also make thread pools
and other fun things very natural. While I'm at it, let me put in my
vote for (as you can see above) non-member functions for anything
that doesn't go into thread_ptr. So thread_ptr could have join() and
detatch() and id() or whatever... And you'd have some non-members like
boost::create_thread(), boost::sleep(), etc... Sticking them in a
class seems very forced, but maybe that's just me. :)


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