Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
* 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

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