0

Please look at following sample code. FuncA runs forever during object lifetime due to while(1). Let's say code hits "CondB" for quite very long time ( 24 hours) or more. which means "curr_status" in funcB will not change during that time. Is it possible that optimization can kick in and never checks for updated value of "curr_status" after that ? Should I use volatile here?

void funcB(string curr_status){
    static string prev_status = "123";
    if(prev_status != curr_status){
        //do sometthing
        prev_status = curr_status;
    }
}

void funcA(){
    while(1){
        if(condA)
            funcB("123");
        if(condB)
            funcB("xyz");
    }
}
  • Did you really mean to put `funcB()` definition inside `funcA()`? – Barmar Feb 02 '17 at 00:12
  • I'm not sure whether to edit, but your code is... odd. Are you missing a closing brace at the end of `funcA`, or are you actually trying to declare `funcB` inside? Also, I'm guessing `sttring` is meant to be `string` – Tas Feb 02 '17 at 00:13
  • No. FuncB is not insde FuncA(). – user3278468 Feb 02 '17 at 00:15
  • The compiler would most likely inline your entire function with hard coded strings in this case. But I'm assuming you have an example like this. – mascoj Feb 02 '17 at 00:15

3 Answers3

4

Is it possible that optimization can kick in and never checks for updated value of "curr_status" after that?

No. Optimization may not change the observable behavior of your program (RVO and calls to potentially overwritten operator new aside). You don't need volatile here.

This is known as the as-if rule.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
1

No, the compiler has no idea if or when the parameter will change, and so will always read it.

-1

volatile is needed for memory that may change by actors outside the scope of the running thread. This may occur when sharing memory between thread or processes or when writing directly to peripherals.

You are running in a single thread and writing to normal RAM so there is no fear that the compiler might optimize out your check.

As an aside, when doing multithreading, once should either use proper locking or atomics and when accessing peripheral memory, linux recommends using readl and writel that are implemented in assembler rather than volatile

doron
  • 27,972
  • 12
  • 65
  • 103
  • That's somewhat misleading to beginners at least. `volatile` does not prevent races from the language's point of view. – Baum mit Augen Feb 02 '17 at 00:37
  • 1
    All volatile is is an instruction to the compiler to make no assumptions about the data in the given memory location and to load it every time it needs it. It does not deal with race conditions and it does not deal with out-of-order reads and writes. – doron Feb 02 '17 at 00:41
  • 1
    @doron: No, it's more than that. Reading or writing a `volatile` variable is _Observable Behavior_, which excludes it from _as-if_ optimizations. That's why `volatile` is unsuitable for multi-threading. Race conditions are unrelated to as-if optimizations. – MSalters Feb 02 '17 at 10:11