pthread_exit, c++ and FATAL: exception not rethrown

Hi,

This is my test-code, a simple program which creates a second thread and terminates it after 5 seconds with pthread_exit. (at least in theory)

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <cxxabi.h>

class MyClass {
    public: MyClass() {printf("MyClass
"); };
            ~MyClass() {printf("~MyClass
"); };
};

extern "C" void *newThread(void *arg) {
    MyClass test;
    try {
        while(1) {
            printf("thread
");
            sleep(5);
            printf("sleep done - pthread_exit
");
            pthread_exit(NULL);
        }
#ifdef BETTER
    } catch (abi::__forced_unwind&) {
        printf("foreced unwind
");
        throw;
#endif
    } catch (const char *s) {
        printf("exception s=%s
",s);
    } catch (...) {
        printf("exception da
");
    }
    printf("thread end
");
    return NULL;
}

int main(int argc, char *argv]) {
    pthread_t th_test;
    if(pthread_create(&th_test, NULL /* const pthread_attr_t *attr */, &newThread, NULL) ) {
        printf("error
");
    }

    sleep(16);
    printf("exit
");
}

let’s compile it with:
g++ pthread_exit.cpp -lpthread

> ./a.out
MyClass
thread
sleep done - pthread_exit
exception da
FATAL: exception not rethrown
Abgebrochen

the program aborts in the line
} catch (…) {

we recompile it with -DBETTER and get:

MyClass
thread
sleep done - pthread_exit
foreced unwind
~MyClass
exit

nice!

so we see:

  1. pthread_exit doesn’t cope with catch (…)
  2. we need catch (abi::__forced_unwind&) throw; to get it work
  3. pthread_exit does stack unwinding and deletes the objects on the stack!
  4. bug: there isn’t even one word about all this in man pthread_exit

is there a way to add this information to the man pages of pthread_exit and pthread_cancel? whom should i contact? imho this behavior should be documented.

greetings from a snowy Austria,
Christian Ferbar

Did you try to put pthread_exit as the last operator of the thread function - outside the try-catch?

pthread_exit() seems to throw an exception of type abi::__forced_unwind. If I catch that exception - either with catch(…) or catch(abi::__forced_unwind&) and do not rethrow that exception abort() will be called. So pthread_exit() outside try-catch works.

Calling pthread_exit from within while cycle may be not a perfect idea.
Maybe this will suit you better:


int terminated = 0
...
extern "C" void *newThread(void *arg) {
    MyClass test;
    while (terminated == 0) {
            printf("thread
");
            sleep(5);
     }
    pthread_exit(NULL);
}

int main(int argc, char *argv]) {
    pthread_t th_test;
    if(pthread_create(&th_test, NULL /* const pthread_attr_t *attr */, &newThread, NULL) ) {
        printf("error
");
    } else {
        sleep(16);
        terminated = 1;
        pthread_join (th_test, NULL);
    }

    printf("exit
");
}