HP aC++/HP C A.06.28 Programmer's Guide Integrity servers (769150-001, March 2014)

Exception Handling
It is illegal to throw out of a thread.
The following example illustrates that you cannot catch an object which has been thrown in a
different thread. To do so will result in a runtime abort since HP aC++ finds no available catch
handler and terminate is called.
#include <pthread.h>
void foo() {
int i = 10;
throw i;
}
int main() {
pthread_t tid;
try {
ret=pthread_create(&tid, 0, (void*(*)(void*))foo, 0);
}
catch(int n) {}
}
Pthreads (POSIX Threads)
Pthreads (POSIX threads) refers to the Pthreads library of thread-management routines. For
information on Pthread routines see the pthread(3t) man page. To use the Pthread routines, your
program must include the <pthreads.h> header file and the Pthreads library must be explicitly
linked to your program.
Example:
aCC -mt prog.c
Limitations
When using STL containers as local objects, the destructor will not get called when pthread_exit
is called, which leads to a memory leak. Do not call pthread_exit from C++. Instead you must
throw or return back to the thread’s initial function. There you can do a return instead of
pthread_exit.
Pthread library has no knowledge of C++ stack unwinding. Calling pthread_exit for will
terminate the thread without doing proper C++ stack unwind. That is, destructors for local objects
will not be called. (This is analogous to calling exit for single threaded program.)
This can be fixed by calling destructors explicitly right before calling pthread_exit.
Example:
#include <pthread.h>
#include <stdlib.h>
#include <exception>
extern "C" int printf(const char*...);
struct A {
A () { printf("ctor called\n"); }
~A () { printf("dtor called\n"); }
};
struct B {
B () { printf("B ctor called\n"); }
~B () { printf("B dtor called\n"); }
};
__thread A* pA; // thread specific data.
void thread_specific_destroy(A *p) {
delete p;
}
typedef void fp(void*);
166 Exception Handling