Quantcast

How to pass a shared_ptr to a thread.

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to pass a shared_ptr to a thread.

jason cipriani-2
Hi, I'm new to boost. I'm using it for the shared_ptr implementation.
I have a usage question. On Windows, a thread function takes a void*
parameter. If I have a shared_ptr to an object, what's the correct way
to pass that to a new thread? E.g.:


DWORD WINAPI threadproc (LPVOID something) {
  // how to get a shared_ptr through "something"?
  ...
}

void create5threads () {
  ...
  shared_ptr p(new Object);
  // what should "something" be if i want to pass p to the new
threads, incrementing its use count?
  CreateThread(..., &threadproc, (LPVOID)something, ...);
  CreateThread(..., &threadproc, (LPVOID)something, ...);
  CreateThread(..., &threadproc, (LPVOID)something, ...);
  CreateThread(..., &threadproc, (LPVOID)something, ...);
  CreateThread(..., &threadproc, (LPVOID)something, ...);
}


Sorry if this is a stupid question, but the only thing I could think
of was "(LPVOID)new shared_ptr(p)", but that means each thread must
explicitly delete the new shared_ptr and that kind of defeats the
purpose!

Thanks,
Jason
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to pass a shared_ptr to a thread.

Steven Watanabe-4
AMDG

Jason Cipriani wrote:

> Hi, I'm new to boost. I'm using it for the shared_ptr implementation.
> I have a usage question. On Windows, a thread function takes a void*
> parameter. If I have a shared_ptr to an object, what's the correct way
> to pass that to a new thread? E.g.:
>
> <snip>
>
> Sorry if this is a stupid question, but the only thing I could think
> of was "(LPVOID)new shared_ptr(p)", but that means each thread must
> explicitly delete the new shared_ptr and that kind of defeats the
> purpose!
>  

One solution would be to use Boost.Thread...

Another would be to have the thread function immediately copy the
shared_ptr to a local variable and delete the one on the heap.

Finally, you could use enable_shared_from this, to allow a shared_ptr
to be recovered from a raw pointer.

In Christ,
Steven Watanabe

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to pass a shared_ptr to a thread.

Scott McMurray-2
In reply to this post by jason cipriani-2
On Wed, Dec 3, 2008 at 22:29, Jason Cipriani <[hidden email]> wrote:
>
> Sorry if this is a stupid question, but the only thing I could think
> of was "(LPVOID)new shared_ptr(p)", but that means each thread must
> explicitly delete the new shared_ptr and that kind of defeats the
> purpose!
>

That's the usual way to pass things through void* callbacks.

If you don't want to do that, then use boost.thread, which does that
trick for you with boost.function so you can use arbitrary functors,
allowing state.

    boost::thread mythread(boost::bind(threadproc, something));
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to pass a shared_ptr to a thread.

jason cipriani-2
In reply to this post by Steven Watanabe-4
On Wed, Dec 3, 2008 at 10:34 PM, Steven Watanabe <[hidden email]> wrote:

> AMDG
>
> Jason Cipriani wrote:
>>
>> Hi, I'm new to boost. I'm using it for the shared_ptr implementation.
>> I have a usage question. On Windows, a thread function takes a void*
>> parameter. If I have a shared_ptr to an object, what's the correct way
>> to pass that to a new thread? E.g.:
>>
>> <snip>
>>
>> Sorry if this is a stupid question, but the only thing I could think
>> of was "(LPVOID)new shared_ptr(p)", but that means each thread must
>> explicitly delete the new shared_ptr and that kind of defeats the
>> purpose!
>>

Thanks for the quick reply.

> One solution would be to use Boost.Thread...

It's too late to refactor, unfortunately.

> Another would be to have the thread function immediately copy the
> shared_ptr to a local variable and delete the one on the heap.

This is the way I'll do it, that's a good idea. It's not what I was
hoping for but I guess it's not that bad, it's easy to understand, and
it does what I want, thanks!

> Finally, you could use enable_shared_from this, to allow a shared_ptr
> to be recovered from a raw pointer.

Hmm, is there a way to do something like this (the following code is broken):


class Item : public enable_shared_from_this<Item> { ... };

DWORD WINAPI threadproc (LPVOID vitem) {
  shared_ptr<Item> s = ((Item *)vitem)->shared_from_this();
  ...
}

void start5threads () {
  shared_ptr<Item> p(new Item);
  CreateThread(..., &threadproc, (LPVOID)p.get(), ...);
  CreateThread(..., &threadproc, (LPVOID)p.get(), ...);
  CreateThread(..., &threadproc, (LPVOID)p.get(), ...);
  CreateThread(..., &threadproc, (LPVOID)p.get(), ...);
  CreateThread(..., &threadproc, (LPVOID)p.get(), ...);
}


... that doesn't require any special synchronization to avoid 'p' in
start5threads going out of scope (and deleting the Item) before 's' is
initialized in any of the threads?

I've got the solution though now, thanks again!
Jason
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to pass a shared_ptr to a thread.

Steven Watanabe-4
AMDG

Jason Cipriani wrote:

>> Finally, you could use enable_shared_from this, to allow a shared_ptr
>> to be recovered from a raw pointer.
>>    
>
> Hmm, is there a way to do something like this (the following code is broken):
>
>
> class Item : public enable_shared_from_this<Item> { ... };
>
> DWORD WINAPI threadproc (LPVOID vitem) {
>   shared_ptr<Item> s = ((Item *)vitem)->shared_from_this();
>   ...
> }
>
> void start5threads () {
>   shared_ptr<Item> p(new Item);
>   CreateThread(..., &threadproc, (LPVOID)p.get(), ...);
>   CreateThread(..., &threadproc, (LPVOID)p.get(), ...);
>   CreateThread(..., &threadproc, (LPVOID)p.get(), ...);
>   CreateThread(..., &threadproc, (LPVOID)p.get(), ...);
>   CreateThread(..., &threadproc, (LPVOID)p.get(), ...);
> }
>
>
> ... that doesn't require any special synchronization to avoid 'p' in
> start5threads going out of scope (and deleting the Item) before 's' is
> initialized in any of the threads?
>  

Oh, right.  Never mind that idea.

In Christ,
Steven Watanabe

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Loading...