#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#define ERROR printf("error @ %s line %d\n", __FILE__, __LINE__ );
void thread_cleanup( void *arg )
{
printf( "cleanup is called\n" );
}
void* threadfn_exception( void *arg )
{
int i = 3;
i /= 0;
return NULL;
}
// FIXME
// endless output: catch a SIGFPE signal
void sigfpe_handler( int sig )
{
printf( "catch a SIGFPE signal\n" );
}
// exceptions in a thread may cause the whole process to be terminated!!
int main(int argc, char const *argv[])
{
pthread_t thread;
// if we set the handler to SIG_IGN/SIG_DFL, the process will be killed
// signal does't work well if portability is taken into consideration, alternative is sigaction(3)
signal( SIGFPE, sigfpe_handler );
if( pthread_create( & thread, NULL, threadfn_exception, NULL ) )
{
ERROR;
return -1;
}
if( pthread_join( thread, NULL ) )
{
ERROR;
return -2;
}
printf("Done!\n");
return 0;
}
转载于:https://my.oschina.net/iCode0410/blog/104568