Boost logo

Boost Users :

From: Nat Goodspeed (nat_at_[hidden])
Date: 2008-01-16 09:44:38


Elli Barasch wrote:

> Is there a way I can "new" a copy of the shared_ptr onto the stack?
> Something like:
>
> pthread_create(&t,&attr,&func,(void *) new shared_ptr<T>(shp));
> //mangled syntax, I know
>
> Something like this would guarantee the object stays in scope?

   Do you control the code that accepts that void* argument?
   Maybe something like the following could work:

1. Before pthread_create(), instantiate a new shared_ptr<Foo> on the
heap. That is, make a heap shared_ptr that points to your target Foo
object. You now have in your hand a plain shared_ptr<Foo>*.

2. Cast your plain shared_ptr<Foo>* to void* and pass it into
pthread_create().

3. In the new thread, cast the opaque void* back to shared_ptr<Foo>*.
Copy the heap shared_ptr<Foo> into a local shared_ptr<Foo>, then delete
the heap shared_ptr<Foo>.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net