Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

atomic arguments order of execution in C

Tags:

c

atomic

I am trying to work with the stdatomic.h functions, specifically atomic_flag_test_and_set. I am not seeing any errors, but want to know if what I am doing is always safe. I have a struct like the following:

typedef struct Mystruct {
    int somedata;
    atomic_flag flag;
} Mystruct;

Later, when I create a mystruct and use its instance of the flag, I do so like this:

if(atomic_flag_test_and_set(&mystructInstance->flag)) {
    // do something
}

Is the evaluation of &mystructInstance->flag always completed before the check for the atomic operation? I would assume so since it should be one processor instruction (or something that emulates one processor instruction), but I want to make sure.

like image 256
Jeff12 Avatar asked Jan 23 '26 18:01

Jeff12


1 Answers

Is the evaluation of &mystructInstance->flag always completed before the check for the atomic operation?

The answer to this question can be found in the section on "Function calls" in the C standard.

6.5.2.2 Function calls
...
4. An argument may be an expression of any complete object type. In preparing for the call to a function, the arguments are evaluated, and each parameter is assigned the value of the corresponding argument.

Also note that if a function takes more than one parameter, the order of evaluation of the arguments passed to it is unspecified. This also is mentioned in the same section in the standard.

10.There is a sequence point after the evaluations of the function designator and the actual arguments but before the actual call. Every evaluation in the calling function (including other function calls) that is not otherwise specifically sequenced before or after the execution of the body of the called function is indeterminately sequenced with respect to the execution of the called function.

like image 110
P.W Avatar answered Jan 25 '26 09:01

P.W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!