From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,dd18429923ec8cb7,start X-Google-Attributes: gidfee84,public From: karthik@cdotd.ernet.in Subject: C++ Exceptions and Thread Cancellation Date: 1999/05/13 Message-ID: <7hf00f$gfe$1@nnrp1.deja.com>#1/1 X-Deja-AN: 477458639 X-Http-Proxy: 1.0 x29.deja.com:80 (Squid/1.1.22) for client 196.1.110.119 Organization: Deja.com - Share what you know. Learn what you don't. X-Article-Creation-Date: Thu May 13 16:52:34 1999 GMT Newsgroups: comp.unix.osf.osf1 X-Http-User-Agent: Mozilla/3.02Gold (X11; I; HP-UX B.10.20 9000/715) Hi, I am posting this here 'cause the problem occurs in Digital Unix 4.0D. Hi, There seems to be a problem in canceling a thread which is in a method which can throw an exception. I have included my program too. It does the following main() creates an object and goes to sleep for 10 seconds The constructor of the object creates a thread The thread sleeps for 600 seconds in a method which can throw an exception. main() wakes up and 'deletes' the object The destructor of the object cancels the object's thread and does a pthread_join() and prints the status. When I execute the program, the main thread does a successful pthread_cancel() and blocks in pthread_join() and the program terminates a bnormally. In Solaris 2.6, it gives Segmentation violation (core dumped) In Digital Unix 4.0D, it gives Resources Lost (core dumped) However, if I compile the program with g++, it works fine. At present, I have compiled g++ only for Digital. So I was able to run it onl y on Digital. The program works fine if I remove the 'throw' declaration of the exception in the declaration of the method inside the class and in the method definition. Pls. note that I DID NOT remove the actual statement which throws the exception. Here is the program. # include # include # include # include # include # include /** ** A test exception. Stores nothing **/ class NewJunkException { public : NewJunkException() {} ~NewJunkException() {} }; /** ** The class which has the thread **/ class Asd { public : Asd(); ~Asd(); ~Asd(); private : void *threadFunc() throw(NewJunkException); static void *threadWrapper(void *); pthread_t thrid; }; /** ** main() ** Creates the object ** Sleeps for 10 seconds ** Deletes the object **/ main() { cout << "main() : Creating object" << endl; Asd *a = new Asd(); cout << "main() : sleep(10)" << endl; sleep(10); cout << "main() : Deleting object" << endl; delete a; } /** ** The constructor. Just creates the thread. **/ Asd:: Asd() { int rval; cout << "Asd:: Asd() : pthread_create(&thrid,...)" << endl; if((rval = pthread_create(&thrid, 0, threadWrapper, this)) != 0) { cout << "Asd:: Asd() : pthread_create() failed; " << strerror(rval) << endl; exit(-1); } } void *Asd:: threadWrapper(void *arg) { try { ((Asd *) arg)->threadFunc(); } catch(NewJunkException e) { } return 0; } /** ** The thread's main function. Sleeps for 600 second ** so that the main thread will be able to send a ** cancel request after it's 10 second sleep. **/ void *Asd:: threadFunc() throw(NewJunkException) { cout << "Asd:: threadFunc() : sleep(600)" << endl; sleep(600); throw NewJunkException(); return 0; } /** ** The destructor ** Cancels the thread ** Does a pthread_join() **/ Asd:: ~Asd() { int rval; cout << "Asd:: ~Asd() : pthread_cancel(thrid)" << endl; if((rval = pthread_cancel(thrid)) != 0) { cout << "Asd:: ~Asd() : pthread_cancel() failed; " << strerror(rval) << endl; exit(-1); } cout << "Asd:: ~Asd() : pthread_cancel(thrid) OVER" << endl; void *status; cout << "Asd:: ~Asd() : pthread_join()" << endl; pthread_join(thrid, &status); cout << "Asd:: ~Asd() : pthread_join()ed; status = " << status << endl; printf("pthread_join()ed : status = %d\n", status); } The changes done to make this work were 1. The declaration void *threadFunc() throw(NewJunkException); was changed to void *threadFunc(); 2. The declaration of the method body void *threadFunc() throw(NewJunkException) was changed to void *threadFunc() The statement throw NewJunkException(); was NOT touched at all. After this change, there is no problem. When compiled with g++ there is no problem even if these changes are not done. How is it that it works with g++ and not with the C++ compilers of Solaris and Digital Unix? I am not sure if it is a thread related problem or an exception related problem or if I am doing something silly. Thanks in advance. karthik@cdotd.ernet.in dkarthikeyan@cdotd.ernet.in --== Sent via Deja.com http://www.deja.com/ ==-- ---Share what you know. Learn what you don't.---