Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch an exception thrown in a critical section?

I'm working on win 32 multithreading with c++. Scenario: I have a function used by multiple threads. This function as a critical sections (or any kind of construct that can lock a resource). In the critical section an exception is thrown. At this point I need to take care of unlocking the resource in the exception catch block.

Is there any other way that this can be done? I mean, let's say that I don't want to have to remember to release the lock in the catch block, is there any common way to handle this problem to avoid this error prone scenario?

like image 750
Uince81 Avatar asked Mar 19 '09 17:03

Uince81


People also ask

What happens when exception is thrown CPP?

If you throw an exception, all functions will be exited back to the point where it finds a try... catch block with a matching catch type. If your function isn't called from within a try block, the program will exit with an unhandled exception.

Can you throw an exception in a catch block C++?

This means that an exception thrown within a catch block will not be caught by the catch block it's in. Instead, it will be propagated up the stack to the caller. The exception thrown from the catch block can be an exception of any type -- it doesn't need to be the same type as the exception that was just caught.

What is exception thrown in C?

C doesn't support exception handling. To throw an exception in C, you need to use something platform specific such as Win32's structured exception handling -- but to give any help with that, we'll need to know the platform you care about. ...and don't use Win32 structured exception handling.

What happens after catching an exception?

It is up to you to ensure that the application is recovered into a stable state after catching the exception. Usually it is achieved by "forgetting" whatever operation or change(s) produced the exception, and starting afresh on a higher level.


1 Answers

The idea is to encapsulate the act of acquiring and releasing the critical section in an object such that constructing the object acquires the CS and destroying the object releases it.

struct CSHolder {
    explicit CSHolder(CRITICAL_SECTION& cs): lock(cs) {
        ::EnterCriticalSection(&lock);
    }
    ~CSHolder() { ::LeaveCriticalSection(&lock); }
    CRITICAL_SECTION& lock;
};


CRITICAL_SECTION gLock;
void foo() {
    CSHolder lockIt(gLock);
    // lock is held until lockIt is destroyed
}

The concept is called RAII - Resource Acquisition is Initialization. It is a very common idiom in modern C++.

like image 92
D.Shawley Avatar answered Oct 15 '22 06:10

D.Shawley