Agreed, sending a raw pointer leaves that window open. Hence my question about "new"ing onto the stack. Cheers, Elli Leon Mlakar wrote:
Wouldn't that open a potential gap for original shared_ptr to destroy whatever is pointed to by raw pointer before thread manages to bind it back to the shared count? .
Cheers,
Leon
------------------------------------------------------------------------ *From:* boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] *On Behalf Of *Ovanes Markarian *Sent:* Wednesday, January 16, 2008 8:31 AM *To:* boost-users@lists.boost.org *Subject:* Re: [Boost-users] Casting a shared_ptr as a prototyped (void *)?
Try using the enable_shared_from_this idiom: http://www.boost.org/libs/smart_ptr/sp_techniques.html#from_this
This will allow you to pass a raw pointer to your thread and later on bind the raw pointer to the correct shared counter.
Good Luck, Ovanes
On Jan 16, 2008 6:45 AM, Elli Barasch
mailto:comptonsw@comcast.net> wrote: Richard Dingwall wrote:
On Jan 16, 2008 5:22 PM, Elli Barasch
mailto:comptonsw@comcast.net wrote: Richard Dingwall wrote:
On Jan 16, 2008 1:11 PM, Elli Barasch
mailto: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
(your_ptr.get()) Richard _______________________________________________ Boost-users mailing list
Boost-users@lists.boost.org mailto: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 mailto: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?
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org mailto:Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
------------------------------------------------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users