Boost logo

Boost Users :

From: STenyaK (Bruno Gonzalez) (stenyak_at_[hidden])
Date: 2007-12-19 07:21:10


Thank you two for the help.

On 12/18/07, Zeljko Vrba <zvrba_at_[hidden]> wrote:
> On Tue, Dec 18, 2007 at 10:28:37AM -0500, Andrew Holden wrote:
> He might just allocate the shared_ptr on the heap (the dynamic memory allocation
> will be anyway cheaper than thread creation):
>
> void *thread(void *ptr)
> {
> shared_ptr<X> *_p = static_cast<shared_ptr<X>*>(ptr);
> shared_ptr<X> p = *_p;
> delete _p;
> ...
> }
>
> void creator()
> {
> ...
> pthread_create(&thrid, 0, thread, new shared_ptr<X>(orig_ptr));
> ...
> }

Ok. This is how my code structure looks:
----------------------------
typedef shared_ptr<MyClass> pMyClass;
void* MyClass::actionThread(void *objectPointer)
{
    pMyClass * _obj = static_cast<pMyClass*>(objectPointer);
    pMyClass obj = *_obj;
    obj->action(false);
    delete _obj;
}
void MyClass::action(bool threaded)
{
    if (threaded)
    {
        pthread_t tid;
        pthread_create(&tid, 0, actionThread, new pMyClass(this))
    } else {
        //TODO: actually perform the action
    }
}
----------------------------
When calling an instance->action(false) it works correctly.
When calling instance->action(true) it sometimes segfaults. So i'm not
sure i interpreted your code sample correctly.

Do i need to use enable_shared_from_this?
Or do i have to avoid using "this" (orig_ptr in your code) somehow maybe?

Thanks in advance!

-- 
Saludos,
     Bruno González
_______________________________________________
Msn/Jabber: stenyak AT gmail.com
ICQ: 153709484
http://www.stenyak.com

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