From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,dd18429923ec8cb7 X-Google-Attributes: gidfee84,public From: "Brian J. McCarthy" Subject: Re: C++ Exceptions and Thread Cancellation Date: 1999/05/19 Message-ID: <37430DCC.9804CF8B@handyman.mv.com> X-Deja-AN: 479729509 Content-Transfer-Encoding: 7bit References: <7hf00f$gfe$1@nnrp1.deja.com> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@mv.com X-Trace: pyrite.mv.net 927141223 24268 207.22.38.9 (19 May 1999 19:13:43 GMT) Organization: MV Communications, Inc. Mime-Version: 1.0 Reply-To: mccarthy@hndymn.zko.dec.com NNTP-Posting-Date: 19 May 1999 19:13:43 GMT Newsgroups: comp.unix.osf.osf1 karthik@cdotd.ernet.in wrote: > > 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. >From the C++ team: The reason that the program doesn't work on Compaq C++ is that when a program receives pthread cancel request, it's delived as an exception. The exception has no type-name right now that's recognized by the C++ exception handling mechanism. It can be caught with catch(...) though. The routine threadFunc has an exception specification that says only NewJunkException can propagate out of it. When pthread cancel is done, the pthread-cancel exception is propagated, which is not expected by the exception specification. As a result, the C++ language routine 'unexpected' is called which aborts the program. This is what we're supposed to do. Someday, we are going to create types for pthread exceptions but until we do, I don't think you can have exception specification on routines which receive pthread cancel exceptions. I also tried adding a "try { } catch (...) {}" around threadFunc, but I think the rules are that you're supposed to propagate the pthread cancel exception and not change it into another type of exception. Possibly g++ implements pthread-cancel with a different mechanism than the exception mechanism (maybe signals). That's why it works for them. Also, I tried Sun C++ 4.2 and they didn't hit the unexpected handler, so I don't know what they do. -- ---------------------------------------------------------------------- Brian J. McCarthy | mailto:brian@handyman.mv.com When this appears, its on my own time ----------------------------------------------------------------------