Are RAII-based stacks guaranteed only after leaving the scope in C ++?

When using Resource Initialization (RIAA) in C ++, it is typical to have something like this:

class CriticalSection {
public:
    void enter();
    void leave();
};

class SectionLocker {
public:
    SectionLocker(CriticalSection& cs)
    : mCs(cs) {
       cs.enter();   
    }

    ~SectionLocker() {
        cs.leave();
    }

private:
    CriticalSection& mCs;
};

CriticalSection gOperationLock; // Global lock for some shared resource

void doThings(int a, int b) {
    SectionLocker locker(gOperationLock);
    int c = doOtherThings(a);
    doMoreThings(b);
    doOneMoreThing(a, b, c);
}

I know that in some garbage collectors (such as the CLR), one of the many reasons why this would be unsafe is because the locker object inside doThings () will have the right to garbage collection before doThings () returns, as a locker never refers after creation.

Is the expected destructor behavior for a gateway called only after doOneMoreThing () is called, correct behavior in C ++?

If so, are there any guarantees as to when the destructor will be called (and gOperationLock will be released)? Or is it only at some point after leaving the sphere of action?

+3
2

++ (n3290) . RAII ( !),

§12.4.11 :

" .... (3.7.3), , (6.7)"

§6.7.2 :

, , (6.6)

§6.6.2:

( ) (3.7.3), , .

, , , , , .

+7

++, RAII . , , , .

+3

All Articles