-
27-Nov-2010, 04:56
#1
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)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <cxxabi.h>
class MyClass {
public: MyClass() {printf("MyClass\n"); };
~MyClass() {printf("~MyClass\n"); };
};
extern "C" void *newThread(void *arg) {
MyClass test;
try {
while(1) {
printf("thread\n");
sleep(5);
printf("sleep done - pthread_exit\n");
pthread_exit(NULL);
}
#ifdef BETTER
} catch (abi::__forced_unwind&) {
printf("foreced unwind\n");
throw;
#endif
} catch (const char *s) {
printf("exception s=%s\n",s);
} catch (...) {
printf("exception da\n");
}
printf("thread end\n");
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\n");
}
sleep(16);
printf("exit\n");
}
let's compile it with:
g++ pthread_exit.cpp -lpthread
Code:
> ./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:
Code:
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
-
27-Nov-2010, 04:56 #1
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)
Code:#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> #include <string.h> #include <cxxabi.h> class MyClass { public: MyClass() {printf("MyClass\n"); }; ~MyClass() {printf("~MyClass\n"); }; }; extern "C" void *newThread(void *arg) { MyClass test; try { while(1) { printf("thread\n"); sleep(5); printf("sleep done - pthread_exit\n"); pthread_exit(NULL); } #ifdef BETTER } catch (abi::__forced_unwind&) { printf("foreced unwind\n"); throw; #endif } catch (const char *s) { printf("exception s=%s\n",s); } catch (...) { printf("exception da\n"); } printf("thread end\n"); 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\n"); } sleep(16); printf("exit\n"); }
g++ pthread_exit.cpp -lpthread
Code:> ./a.out MyClass thread sleep done - pthread_exit exception da FATAL: exception not rethrown Abgebrochen
} catch (...) {
we recompile it with -DBETTER and get:
Code:MyClass thread sleep done - pthread_exit foreced unwind ~MyClass exit
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