UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C++
  • » C++ objects and thread cancellation (pthread)

#1 2010-11-20 01:06 PM

tomcio
Member
From: Poland
Registered: 2006-08-14
Posts: 113

C++ objects and thread cancellation (pthread)

Hello!

I'm working on C++ multi-threading application. I read somewhere, that when thread (pthread implementation on Linux) is canceled it doesn't unwind stack and doesn't call destructors for C++ objects. is it true?

I created a workaround:

typedef std::tuple<ClassA*, ClassB*> CppObjectsTuple;

void cpp_data_cleanup (void* data)
{
    CppObjectsTuple* cpp_objects_tuple (static_cast<CppObjectsTuple> (data));
    ClassA*              object_a (std::get<0> (*cpp_objects_tuple));
    ClassB*              object_b (std::get<1> (*cpp_objects_tuple));

    object_a->~ClassA ();
    object_b->~ClassB ();
    cpp_objects_typle->~tuple ();
}

void* thread_function (void* data);
{
    ClassA              object_a;
    ClassB              object_b;
    CppObjectsTuple cpp_objects_tuple (&object_a, &object_b);

    pthread_cleanup_push (cpp_data_cleanup);
    // ... other code...
    pthread_cleanup_pop (0); // do not call cleanup function
}

What do you think about this? Maybe there's a better way to do this?

Offline

#2 2010-11-20 04:19 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: C++ objects and thread cancellation (pthread)

I don't think that's true, that would be pretty stupid. Technically there is
no reason whatsoever to not do it either.

And according to the manpage calling destructors does happen.

Your work around won't work because if the pthread implementation
is so brain dead that it can't call destructors, it won't be able to call
your cleanup functions either.

Offline

#3 2010-11-20 05:52 PM

tomcio
Member
From: Poland
Registered: 2006-08-14
Posts: 113

Re: C++ objects and thread cancellation (pthread)

Offline

#4 2010-11-22 01:33 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: C++ objects and thread cancellation (pthread)

gcc should ignore everything after return too, and exit() and any other function with __attribute__((__noreturn__)).

Offline

  • Index
  • » C++
  • » C++ objects and thread cancellation (pthread)

Board footer

Powered by FluxBB