467,116 Members | 1,155 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,116 developers. It's quick & easy.

Unknown exception occured for pthread_exit()

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");
}
}


Nov 28 '07 #1
  • viewed: 4192
Share:
4 Replies
le*******************@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.
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 28 '07 #2
le*******************@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.
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;
}
--
IYesNo yes=YesNoFactory.getFactoryInstance().YES;
yes.getDescription().equals(array[0].toUpperCase());
Nov 28 '07 #3
Marco Manfredini wrote:
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.
--
IYesNo yes=YesNoFactory.getFactoryInstance().YES;
yes.getDescription().equals(array[0].toUpperCase());
Nov 28 '07 #4
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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 29 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Ritz, Bruno | last post: by
2 posts views Thread by jon morgan | last post: by
reply views Thread by Nick | last post: by
5 posts views Thread by Jacek Dziedzic | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.