Boost logo

Boost Users :

Subject: Re: [Boost-users] How to pass a shared_ptr to a thread.
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-12-03 22:34:23


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 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