1

As I understood, std::lock locks section under its scope. But what if access to variable is performed in return statement? Example:

// Looks safe...
bool foo
{
    std::lock_guard<decltype(myMutex)> tLock(myMutex);
    if (bar == 123)
    {
        return true;
    }
    else
    {
        return false;
    }
}

// Is it safe?
bool foo
{
    std::lock_guard<decltype(myMutex)> tLock(myMutex);
    return bar == 123;
}
artsin
  • 1,434
  • 2
  • 13
  • 29

1 Answers1

2

Destructor is called after the return,

so you are fine in both cases.

Jarod42
  • 203,559
  • 14
  • 181
  • 302