Boost logo

Boost :

From: Greg Colvin (gcolvin_at_[hidden])
Date: 2001-06-30 15:07:24


From: Greg Colvin <gcolvin_at_[hidden]>
> From: Peter Dimov <pdimov_at_[hidden]>
> > From: "Peter Dimov" <pdimov_at_[hidden]>
> >
> > [Replying to myself... What's next? A dedicated mailing list just for me?
> > :-) ]

And now I'll reply to myself replying to Peter replying to himself.

> > > So my point is that a good implementation of thread::ref can directly
> > > compete with the 'layer 1' noncopyable thread object and be an order of
> > > magnitude more efficient than 'layer 2' shared_ptr<thread>, while at the
> > > same time being more user-friendly.
> >
> > Reality check.
> >
> > The above is true with regard to thread-related resources, but it ignores
> > the problem with the lifetime of the arbitrary function object that is
> > passed to thread::create.
> >
> > A design with
> >
> > thread::ref thread::create(void (*) (void *), void *);
> >
> > plus the corresponding helpers for managing the parameter passing and
> > dealing with different function signatures still has the properties outlined
> > above.
> >
> > A design that takes boost::function0<void>, sadly, does not, as far as I can
> > see. Someone has to manage the function object, and the logical place to put
> > it is in the noncopyable thread.
>
> One could just copy it around as part of the ref object.

Even better, you just put a copy on the stack as part of the wrapper
function that you have to generate to pass to the native thread
creation function. The thread::ref provides no access to this
function object, so it needs no lifetime beyond the thread itself.
In Windows, something like:

   namespace thread {

      namespace detail {
         void __cdecl thread::fun(void* arg) {
            try {
               (*(boost::function0<void>*)arg)();
            } catch(...) {
            terminate();
            }
         }
      }

      ref create(boost::function0<void> f) {
         return ref(_beginthread(detail::fun,(void*)&f));
      }

      class ref {
         HANDLE id;
         ref(HANDLE id) : id(id) {}
         ...
      };
   }


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