Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guarantee function call in logical AND expression [duplicate]

I am in the process of refactoring some code using C++ atomics. The code looks like this:

std::atomic<bool> someFlag{}; // This can be set to true using a public method

// ...

const bool cond1 { someFunction() };
const bool cond2 { otherFunction() };

if (someFlag.load())
{
    someFlage.store(false);

    if (cond1 && cond2)
    {
        performSomeAction();
    }
}

I'm currently planning to rewrite the if statement like this:

if (std::atomic_exchange(&someFlag, false) &&
    cond1 && cond2)
{
    performSomeAction();
}

What is extremely important, is that after this if statement, the someFlag variable is set to false. I therefore want to make sure that the call to atomic_exchange always occurs, regardless of the value of cond1 and cond2. Can I be guaranteed that this will be the case since boolean expressions are evaluated left to right, regardless of optimisation settings?

like image 474
op414 Avatar asked Jan 14 '20 10:01

op414


Video Answer


2 Answers

Yes, the order is guaranteed. From cppreference.com:

Every value computation and side effect of the first (left) argument of the built-in logical AND operator && and the built-in logical OR operator || is sequenced before every value computation and side effect of the second (right) argument.

like image 153
Renat Avatar answered Nov 11 '22 07:11

Renat


In if (std::atomic_exchange(&someFlag, false) && cond1 && cond2)

  • std::atomic_exchange(&someFlag, false) will be called first.

  • If evaluate to true, evaluates cond1

  • If cond1 is true, evaluates cond2.

  • and finally performSomeAction() if cond2 is also true.

like image 45
Jarod42 Avatar answered Nov 11 '22 07:11

Jarod42