Re: Unknown exception occured for pthread_exit()
On Nov 28, 3:06 pm, leelaprasad.gorrep...@gmail.com wrote:
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.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34