pthreads and exceptions

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

pthreads and exceptions

Jon Rafkind
Hi,
    I am experiencing an issue with pthreads 2.8.0 and c++ exceptions.
If main spawns thread A and thread A spawns thread B then thread A
throws an exception and tries to catch it, thread B will actually catch
it. I have tried libpthreadGC2.dll and libpthreadGCE2.dll but the
behavior is the same. Is there a way to make right the stack unwinding
behavior match that as on Linux?

Here is an example program which shows the behavior:

#include <stdio.h>
#include <exception>
#include <pthread.h>

using namespace std;

class Fex: public exception{
public:
        Fex():exception(){}
};

void * d1_child( void * x ){
        try{
                int i;
                printf( "d1 child running\n" );
                for ( i = 0; i < 200000000; i++ ){
                }
        } catch ( const Fex & f ){
                printf( "d1 child fex\n" );
        }
        printf( "d1 child done\n" );
}

void * d1( void * x ){
        try{
                pthread_t child;
                pthread_create( &child, NULL, d1_child, NULL );
                int i;
                for ( i = 0; i < 100000000; i++ ){
                }
                printf( "d1 throwing fex\n" );
                throw Fex();
                pthread_join( child, NULL );
                printf( "d1 joined child\n" );
        } catch ( const Fex & f ){
                printf( "d1 fex\n" );
        }
        printf( "d1 done\n" );
        return NULL;
}

void * d2( void * x ){
        try{
                throw Fex();
        } catch ( const Fex & f ){
                printf( "d2 fex\n" );
        }
        return NULL;
}

int main(){
        pthread_t n1, n2;
        pthread_create( &n1, NULL, d1, NULL );
        pthread_create( &n2, NULL, d2, NULL );

        pthread_join( n1, NULL );
        pthread_join( n2, NULL );
}

This will print

d2 fex
d1 child running
d1 throwing fex
d1 child fex
d1 child done

thanks,
jon