Boost logo

Boost :

From: williamkempf_at_[hidden]
Date: 2001-07-02 08:39:07


--- In boost_at_y..., "Alexander Terekhov" <terekhov_at_d...> wrote:
>
> > 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) {}
> > ...
> > };
> > }
>
> you are passing the address of creator's _local_
> variable to another (new) thread. you can not do
> it without extra synchronization. in the code above
> the local variable "f" could be reclaimed
> (create() returns) even before new thread would
> have a chance to use it in thread::fun. the code
> above is incorrect.
>
> correct solution (w/o extra synch) could be to "copy"
> "f" into C++ thread _OBJECT_ and let him manage it.

Ahem. That's only one correct solution. Beyond using extra synch
code you can also just copy f onto the heap and let the
detail::thread::fun() delete it.

Bill Kempf


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