jeudi 6 avril 2017

Compiler optimizations for equality checked stores

Consider this code snippet:

static std::atomic<bool> var; //the variable to update.

// Update() updates the variable `var`
void Update() {
     bool result = Foo(); //Foo() is some function returning a boolean.

     // The doubt is here in this conditional store.
     if (result != var) {
         var = result;
     }
}

If Update() is called frequently, say multiple threads, one wouldn't want to store result to var if already var == result due to cache failures.

The question follows:

  1. Does if (result != var) var = result get optimized to var = result ?

  2. Does this optimization (if any) differ if var is not an atomic?

Aucun commentaire:

Enregistrer un commentaire