简体   繁体   中英

Is it possible to “Turn off” or “Turn on” volatility in C variables?

I have two static volatile variables in C and I would like to check them both in a logical statement. However, when I do I get a warning " undefined behavior: the order of volatile accesses is undefined in this statement 1037" Is it possible to suspend volatility of a C variable for a very brief moment to ensure good data?

Here is the code:

static volatile unsigned char b;
static volatile unsigned char a;

//update the states of the two volatile variables 
update_vars( &a);
update_vars( &b);

// check them in a logical statement
// Can I suspend the volatile lable??  
if((addr_bit & (a | b)) == 0){
// update another variables
}
else{
// another action
}

I'm thinking about this in the same context of interrupts, whereas you temporary suspend them if you want stable evaluation of the data at a precise moment. Thanks!

The volatile property of a variable cannot be disabled.

You need to create a non-volatile copy of each and then operate on those.

unsigned char a_stable = a;
unsigned char b_stable = b;

if((addr_bit & (a_stable | b_stable)) == 0){
    ...

为了避免警告,您可以分解C语句,以使每个C语句仅具有对volatile变量的一次访问。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM