Casting a shared_ptr as a prototyped (void *)?
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?
On Jan 16, 2008 1:11 PM, Elli Barasch
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
Richard Dingwall wrote:
On Jan 16, 2008 1:11 PM, Elli Barasch
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 http://lists.boost.org/mailman/listinfo.cgi/boost-users
Super. Thanks for the quick response!
Richard Dingwall wrote:
On Jan 16, 2008 1:11 PM, Elli Barasch
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 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. Thanks, Elli that the shared pointer reference count could be
On Jan 16, 2008 5:22 PM, Elli Barasch
Richard Dingwall wrote:
On Jan 16, 2008 1:11 PM, Elli Barasch
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 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
Richard Dingwall wrote:
On Jan 16, 2008 5:22 PM, Elli Barasch
wrote: Richard Dingwall wrote:
On Jan 16, 2008 1:11 PM, Elli Barasch
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 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?
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
Richard Dingwall wrote:
On Jan 16, 2008 5:22 PM, Elli Barasch
wrote: Richard Dingwall wrote:
On Jan 16, 2008 1:11 PM, Elli Barasch
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.orghttp://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 listBoost-users@lists.boost.orghttp://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 http://lists.boost.org/mailman/listinfo.cgi/boost-users
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
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
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>.
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));
The easiest way to do that would be to use Boost.Threads. void func( shared_ptr<T> p ); boost::thread t( boost::bind( func, shp ) ); If you need to use pthread_create, you might want to look at the source of Boost.Threads to see how it implements this functionality. That said, your suggested approach can also work; you'd need to cast the void* in func back to shared_ptr<T>* ppt and remember to delete it once you copy *ppt to a local shared_ptr variable.
participants (6)
-
Elli Barasch
-
Leon Mlakar
-
Nat Goodspeed
-
Ovanes Markarian
-
Peter Dimov
-
Richard Dingwall