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:
- pthread_exit doesn’t cope with catch (…)
- we need catch (abi::__forced_unwind&) throw; to get it work
- pthread_exit does stack unwinding and deletes the objects on the stack!
- 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