<p>I have an application where 2 threads are running... Is there any certanty that when I change a global variable from one thread, the other will notice this change? 
I don't have any syncronization or Mutual exclusion system in place... but should this code work all the time (imagine a global <strong>bool</strong> named <strong>dataUpdated</strong>):</p>

<p>Thread 1:</p>

<pre><code>while(1) {
    if (dataUpdated)
        updateScreen();
    doSomethingElse();
}
</code></pre>

<p>Thread 2:</p>

<pre><code>while(1) {
    if (doSomething())
        dataUpdated = TRUE;
}
</code></pre>

<p>Does a compiler like gcc optimize this code in a way that it doesn't check for the global value, only considering it value at compile time (because it nevers get changed at the same thred)?</p>

<p>PS: Being this for a game-like application, it really doen't matter if there will be a read while the value is being written... all that matters is that the change gets noticed by the other thread.</p>

Use the
volatile
keyword to hint to the compiler that the value can change at any time.
```
```
volatile int myInteger;
```
```