Boost logo

Boost Users :

From: Elli Barasch (comptonsw_at_[hidden])
Date: 2008-01-16 09:06:43


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_at_[hidden]
> [mailto:boost-users-bounces_at_[hidden]] *On Behalf Of *Ovanes
> Markarian
> *Sent:* Wednesday, January 16, 2008 8:31 AM
> *To:* boost-users_at_[hidden]
> *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 <comptonsw_at_[hidden]
> <mailto:comptonsw_at_[hidden]>> wrote:
>
> Richard Dingwall wrote:
>> On Jan 16, 2008 5:22 PM, Elli Barasch <comptonsw_at_[hidden]> <mailto:comptonsw_at_[hidden]> wrote:
>>
>>> Richard Dingwall wrote:
>>>
>>> On Jan 16, 2008 1:11 PM, Elli Barasch <comptonsw_at_[hidden]> <mailto: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] <mailto: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] <mailto: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 mailing list
> Boost-users_at_[hidden] <mailto:Boost-users_at_[hidden]>
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users



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