Click to See Complete Forum and Search --> : Exceptions within a thread func


kempofighter
August 25th, 2005, 07:19 PM
I can't quite find an answer to this question so I thought I'd post an example.

I found some code where someone throws an exception from within a posix thread func. I don't understand how it could be caught and what an operating system would do if a throw unwinds to the beginning of a static thread func without being caught. Will the program simply crash at this point? Here is an example

void funcThatLaunchesThread()
{

pthread_t spawnedThread;
short result = pthread_create(&spawnedThread,
NULL,
spawnWorkRoutine,
(void *)this);
}

void * DataDevice::spawnWorkRoutine(void * dataPtr)
{
try
{
initReceiveLMChannel();
}
catch(...)
{
throw std::exception("work routine failed");
}
}

I realize I haven't given complete details. I'm only interested, conceptually, in the idea of the throw within the threadfunc. The spawnWorkRoutine is the static function that is managed by the OS after I launch the thread. So if someone catches an exception do they really want to throw another one? I would think you'd want to prevent exceptions from unwinding any further; similar to preventing exceptions from propagating out of destructors. Am I right in seeing a problem here or is this code okay?

My perspective is that I am new to exception handling and having to review a lot of existing code within an existing program. So I am trying to understand this and determine if I need to change it. It seems like I would want to just end the thread and set an error code rather than throw from within the thread. Am I missing something?

proghelper
August 29th, 2005, 04:45 AM
I found some code where someone throws an exception from within a posix thread func. I don't understand how it could be caught and what an operating system would do if a throw unwinds to the beginning of a static thread func without being caught.

I think that you can catch this exception in the main program where the threads are invoked. However, in this case I am assuming that the main program is going to wait till the threads complete.


==========================

Programming ( Assignment / Project ) Help (http://www.programminghelp4u.com/)