Boost logo

Boost Users :

From: Elli Barasch (comptonsw_at_[hidden])
Date: 2008-01-16 00:45:31


Richard Dingwall wrote:
> On Jan 16, 2008 5:22 PM, Elli Barasch <comptonsw_at_[hidden]> wrote:
>
>> Richard Dingwall wrote:
>>
>> On Jan 16, 2008 1:11 PM, Elli Barasch <comptonsw_at_[hidden]> 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_at_[hidden]
>> 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_at_[hidden]
> 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 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