* C++ Exceptions and Thread Cancellation
@ 1999-05-13 4:00 karthik
1999-05-18 4:00 ` Brian J. McCarthy
1999-05-19 4:00 ` Brian J. McCarthy
0 siblings, 2 replies; 3+ messages in thread
From: karthik @ 1999-05-13 4:00 UTC (permalink / raw)
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 <pthread.h>
# include <stdio.h>
# include <unistd.h>
# include <string.h>
# include <stdlib.h>
# include <iostream.h>
/**
** 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.---
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: C++ Exceptions and Thread Cancellation
1999-05-13 4:00 C++ Exceptions and Thread Cancellation karthik
@ 1999-05-18 4:00 ` Brian J. McCarthy
1999-05-19 4:00 ` Brian J. McCarthy
1 sibling, 0 replies; 3+ messages in thread
From: Brian J. McCarthy @ 1999-05-18 4:00 UTC (permalink / raw)
karthik@cdotd.ernet.in wrote:
> I am posting this here 'cause the problem occurs in Digital Unix 4.0D.
> 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
[ major snip ]
Thanks for the example - we'll take a look at it.
--
-----------------------------------------------------------------------
Brian J. McCarthy | mailto:Brian.J.McCarthy@compaq.com
Compaq Computer Corp. Nashua, NH | Compaq C/C++ Runtime Libraries
-----------------------------------------------------------------------
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: C++ Exceptions and Thread Cancellation
1999-05-13 4:00 C++ Exceptions and Thread Cancellation karthik
1999-05-18 4:00 ` Brian J. McCarthy
@ 1999-05-19 4:00 ` Brian J. McCarthy
1 sibling, 0 replies; 3+ messages in thread
From: Brian J. McCarthy @ 1999-05-19 4:00 UTC (permalink / raw)
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 <pthread.h>
> # include <stdio.h>
> # include <unistd.h>
> # include <string.h>
> # include <stdlib.h>
>
> # include <iostream.h>
>
> /**
> ** 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
----------------------------------------------------------------------
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~1999-05-19 4:00 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1999-05-13 4:00 C++ Exceptions and Thread Cancellation karthik
1999-05-18 4:00 ` Brian J. McCarthy
1999-05-19 4:00 ` Brian J. McCarthy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox