user246181
user246181

Reputation: 17

Proper way to catch boost thread interrupt and exit thread

I have the code below which is the start of a new thread. BeginWork function goes through a sequence of functions that have heavy exception handling. If threadgroup.interrupt_all(); is called the individual function will handle the interrupt and this causes the thread to remain in it's loop without being interrupted properly. How can I change my error handling to account for this type of exception and mimic the behavior as if the outer try catch block of the intial loop caught the thread interrupt exception? I'm thinking maybe catch the exception of time interupt and set a boolean to true which is checked within the main loop and break if it is set to true.

void ThreadWorker(int intThread)
{
    try
    {
        int X = 0;


        do
        {
            cout << "Thread " << intThread << "\n";
            BeginWork();
        }
        while (true);

    }

    catch(...)
    {
        cout << "Thread: " << intThread << " Interrupted.";
    }
}

Upvotes: 1

Views: 1198

Answers (1)

sliser
sliser

Reputation: 1645

You can easily do it by boost::thread_interrupted rethrowing in a function catch site

try
{
    // data processing
}
catch(const boost::thread_interrupted &)
{
    throw;
}
catch(...)
{
    // exceptions suppression
}

But i think it's not a very good idea to handle all exceptions inside every function. Better way is to handle only specific exceptions, allowing others to propagate further to the top level function, where they will be handled.

Maybe this site will be useful for you

Upvotes: 1

Related Questions