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:52:38


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