Richard Dingwall wrote:
On Jan 16, 2008 5:22 PM, Elli Barasch <comptonsw@comcast.net> wrote:
  
 Richard Dingwall wrote:

 On Jan 16, 2008 1:11 PM, Elli Barasch <comptonsw@comcast.net> wrote:


 I'd like to pass a shared_ptr as the entry argument to a thread function
via pthread_create. However,
the entry argument is prototyped as void *. I can't simply cast the
shared pointer as a (void *). How do I go about this?

 Try passing:

static_cast<void *>(your_ptr.get())


Richard
_______________________________________________
Boost-users mailing list

Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users


 A follow up question.  Does get() increase the reference count, or does it
simply return the underlying pointer without doing so?  If so, I'd be
worried that a race exists such that the object could be destroyed before
the thread gets to run.
    

get() does not incremement the reference count, as it returns a raw
pointer, and there is no way of knowing how a raw pointer is being
consumed or stored.

If you the function you are passing it to retains a reference to the
pointer anywhere, there is no way this can be tracked by the
shared_ptr. Furthermore, if the shared_ptr(s) go out of scope the raw
pointer references will be left dangling.

HTH,

Richard
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

  
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?