Unknown exception occured for pthread_exit()

  • Thread starter leelaprasad.gorrepati
  • Start date
L

leelaprasad.gorrepati

We had written an application in which we create worker thread.

So the main thread will create the worker thread. After some time
the child thread(Worker thread) will call pthread_exit().
This function was written in try{} and there occured an and is handled
in catch(...) handler.

Can any one tell me the reason why the exception occured.

This is on Linux platform.


The Implementation of the code is as follows:
This is a simple form of our application, Try to run this small piece
of code.

The output of this is " FATAL: exception not rethrown
Inside catch... Aborted"

#include <stdio.h>
#include <stdlib.h>
#include <stdexcept.h>
#include <pthread.h>

using namespace std;

void print_message_function( void *ptr );
int main()
{
pthread_t thread1;
char *message1 = "Thread 1";
int iret1;

iret1 = pthread_create( &thread1, NULL, (void*(*)
(void*))&print_message_function, (void*) message1);

pthread_join( thread1, NULL);


}

void print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;

try
{
pthread_exit((void*) 1);
}
catch(...)
{
printf("Inside catch...\n");
}
}
 
V

Victor Bazarov

We had written an application in which we create worker thread.

So the main thread will create the worker thread. After some time
the child thread(Worker thread) will call pthread_exit().
This function was written in try{} and there occured an and is handled
in catch(...) handler.

Can any one tell me the reason why the exception occured.

This is on Linux platform.

Somebody in the Linux newsgroup probably can [tell you the reason].

Another possibility to find some information is to post to a POSIX
newsgroup, since you're using POSIX threads. But I'd go with the
Linux newsgroup first.

V
 
M

Marco Manfredini

We had written an application in which we create worker thread.

So the main thread will create the worker thread. After some time
the child thread(Worker thread) will call pthread_exit().
This function was written in try{} and there occured an and is handled
in catch(...) handler.

Can any one tell me the reason why the exception occured.

I give you a rough picture, because the posix people usually claim that
they've never heard of C++:

Some implementations of pthreads support cleanup of C++ stackframes
during cancellation. They basically throw a hidden exception back to
the routine that launched the thread procedure, thereby invoking all
destructors. catch(...) catches this exception, of course, may perform
cleanup, but must rethrow the exception, because flowing out out the
handler would mean that the cancellation is cancelled, which isn't
supported.

So for you, this is correct:
catch(...)
{
printf("Inside catch...\n");
throw;
}
 
M

Marco Manfredini

Marco said:
during cancellation. They basically throw a hidden exception back to
the routine that launched the thread procedure, thereby invoking all
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Not *your* routine, some routine inside of the pthreads library that
invoked the function pointer you gave to pthread_create.
 
J

James Kanze

We had written an application in which we create worker thread.
So the main thread will create the worker thread. After some time
the child thread(Worker thread) will call pthread_exit().
This function was written in try{} and there occured an and is handled
in catch(...) handler.
Can any one tell me the reason why the exception occured.
This is on Linux platform.
The Implementation of the code is as follows: This is a simple
form of our application, Try to run this small piece of code.

This is an OS issue; you'll have to ask in a Linux group. (The
code works perfectly well under Solaris, both with g++ and with
Sun CC.) Off hand, I'd guess there is an error somewhere in
either the OS or the g++ implementation on that platform which
causes some interaction between exceptions and thread
cancelation.
The output of this is " FATAL: exception not rethrown
Inside catch... Aborted"

For what it's worth, the code below isn't legal C++, and won't
generally compile. (I couldn't get it to compile anywhere
without some modifications, and in other places, g++ only
compiles it because of a bug.)
#include <stdio.h>
#include <stdlib.h>
#include <stdexcept.h>

There's no such header.
#include <pthread.h>
using namespace std;
void print_message_function( void *ptr );

And since you pass the address of this function to
pthread_create, the signature must be:

extern "C" void* print_message_function( void* ) ;

Without the `extern "C"', the code won't compile with a
conformant compiler. (This is a known bug in g++.) And having
the function return void, then casting the pointer, causes
undefined behavior.
int main()
{
pthread_t thread1;
char *message1 = "Thread 1";

And of course, this should be
char const* message1 = ...
(even if it requires a const_cast in the pthread_create:).)
int iret1;
iret1 = pthread_create( &thread1, NULL, (void*(*)
(void*))&print_message_function, (void*) message1);

The fact that you require the casts here should tell you that
you've done something wrong.
pthread_join( thread1, NULL);
}
void print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
try
{
pthread_exit((void*) 1);
}
catch(...)
{
printf("Inside catch...\n");
}

}

More generally, the interaction between pthread_cancel and
destructors isn't well defined (and pthread_exit() does the
equivalent of a pthread_cancel); the behavior is different
between g++ and Sun CC, even under Solaris. In the end, I'd say
that it's better to avoid them, and just return from your thread
function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top