Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,314 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,078 people online right now. Registration is fast and FREE... Join Now!




Stopping a multithreaded Process when one thread fails

 
Reply to this topicStart new topic

Stopping a multithreaded Process when one thread fails

SpiderSpartan
post 13 Mar, 2008 - 07:09 AM
Post #1


D.I.C Head

Group Icon
Joined: 6 Feb, 2008
Posts: 66



Thanked 3 times

Dream Kudos: 25
My Contributions


I'm new to thread programming and need some help with an issue. I have a program that uses two threads to do various tasks. For some reason one of these threads, on occasion, fails and causes an "Instruction at 0x#### referenced memory at 0x####. The memory could not be written" error. Since I did not write the program, cannot force a reproduction of the error, and have not had a chance to debug the error when it has appeared I have no way of tracing exactly what is causing the error. The problem is that when a thread of a multithreaded application causes an exception in Win32, the system displays a dialog box. Until the user responds to the box, other threads of the application continue running. Once the user has responded, all threads of the application terminate.
I'm wondering, is there any way to catch the thread failure right away instead of waiting for a user response and end the process or stop the other thread from continuing to run? Thanks for any help.
User is offlineProfile CardPM

Go to the top of the page

skaoth
post 13 Mar, 2008 - 09:49 AM
Post #2


D.I.C Regular

Group Icon
Joined: 7 Nov, 2007
Posts: 337



Thanked 9 times

Dream Kudos: 100
My Contributions


The error you are seeing is the default Structured Exception Handler (SEH) that
win32 has inserted into the thread. The error message you are seeing is the
default behavior of exception handler.

What you need to do is either wrap the code that is causing the exception
around a new SEH or wrap the whole thread in a SEH.

The SEH code looks like this
CODE

__try
{
   // Threading code
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
    // Handle exception
}


Source:
http://www.microsoft.com/msj/0197/exception/exception.aspx

hope that helps
User is offlineProfile CardPM

Go to the top of the page

SpiderSpartan
post 13 Mar, 2008 - 10:53 AM
Post #3


D.I.C Head

Group Icon
Joined: 6 Feb, 2008
Posts: 66



Thanked 3 times

Dream Kudos: 25
My Contributions


Thanks for the reply. I've used the a little SEH before. If another function is called inside a try statement, and that called function has an error, would that be returned to the calling try statement. ie:

CODE

try
{
     ProcessStuff();
}
catch(...)
{
     "Something failed code";
}

void ProcessStuff()
{
     "Some code that causes an error";
}


any error that occurred inside the ProcessStuff function would still be caught, Right? Thanks
User is offlineProfile CardPM

Go to the top of the page

skaoth
post 13 Mar, 2008 - 11:15 AM
Post #4


D.I.C Regular

Group Icon
Joined: 7 Nov, 2007
Posts: 337



Thanked 9 times

Dream Kudos: 100
My Contributions


If I understand you correctly then yes.
If an exception occurs in ProcessStuff()
and the exception handler in that function does not handle
the exception, that exception will propagate up the
stack until a handler for the exception is found.

In your example the catch(...) is set to
catch and handle any exception that ProcessSuff()
does not.

However, the c++ exception handler does not catch certain types of
exception. In your original post you stated you were getting a memory
access violation. The standard c++ exception mechanism does not catch these types of exceptions, nor things like divide by zero.

Here is an example

CODE


void Process()
{
    int *pInt = (int*)0x00000000;
        
    // Try and access the memory
    *pInt = 44;
}


int main(int argc, char *argv[])
{
    // SEH
    // This will be caught
    __try
    {
        Process();
    }
    __except(EXCEPTION_EXECUTE_HANDLER)
    {
        std::cout << "caught exception with SEH handler\n";
    }

    // C++ exception handling
    // The exception generated by Process()
    // will not be handled

    try
    {
        Process();
    }
    catch(...)
    {
        std::cout << "caught exception with c++ exception handler\n";
    }
}


Note: The code above above won't compile correctly
MSVC++ only allows 1 type of exception handling per
function. So just comment out 1 set of try/catch to test and you
will see that the __try/__except handles memory
access violations but the try/catch does not. It will
display the message box you were talking about
in your 1st post.

This post has been edited by skaoth: 13 Mar, 2008 - 11:18 AM
User is offlineProfile CardPM

Go to the top of the page

SpiderSpartan
post 13 Mar, 2008 - 11:48 AM
Post #5


D.I.C Head

Group Icon
Joined: 6 Feb, 2008
Posts: 66



Thanked 3 times

Dream Kudos: 25
My Contributions


QUOTE(skaoth @ 13 Mar, 2008 - 12:15 PM) *

Note: The code above above won't compile correctly
MSVC++ only allows 1 type of exception handling per
function. So just comment out 1 set of try/catch to test and you
will see that the __try/__except handles memory
access violations but the try/catch does not. It will
display the message box you were talking about
in your 1st post.


I can't use try and __try in the same function, but if I nest them then the error should be passed back up until it is handled right? Thank you so much for all your help.
User is offlineProfile CardPM

Go to the top of the page

skaoth
post 13 Mar, 2008 - 12:10 PM
Post #6


D.I.C Regular

Group Icon
Joined: 7 Nov, 2007
Posts: 337



Thanked 9 times

Dream Kudos: 100
My Contributions


Once an exception handler handles an exception, the execution generally
continues after the exception handler block. If you want to propagate the exception
then you need to explicitly "rethrow" the exception.

From your comments you are trying to catch all errors that may occur, is this correct?
And what do you mean by nesting?

This will not work. Nor will the reverse. As I stated you _try/_except cannot be
used in the same function.
CODE

try
{
    __try
    {
        ....
    }
    __except(EXCEPTION_EXECUTE_HANDLER)
    {
        // Handle exception
    }
}catch(...) {}



You can do this. I'm sure there might be other ways to handle
both SEH and c++ exceptions, but I'm not an expert at that
CODE

void Process()
{
    __try
    {
        int *pInt = (int*)0x00000000;
        
        // Try and access the memory
        *pInt = 44;
    }__except(EXCEPTION_EXECUTE_HANDLER)
    {
        std::cout << "caught exception with SEH handler\n";

        // Rethrow the exception so that the c++ handler
        // in the calling function handles the
        // error also
        throw exception("error occurred");
    }
}

int main(int argc, char *argv[])
{
    // C++ exception handling
    // The exception generated by Process()
    // will not be handled

    try
    {
        Process();
    }
    catch(...)
    {
        std::cout << "caught exception with c++ exception handler\n";
    }


The output will be
caught exception with SEH handler
caught exception with c++ exception handler
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 02:18AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month