Let's say that I have two threads that share the global variable x.
Thread A's job is to set a value to x, and Thread B's job is to read x. Now each thread (or each core I suppose) will have a cached copy of x.
Let's say that Thread A has set the value of x to 12345. Now Thread A's cache could remain unaltered because the CPU can schedule the set operation to be executed later, and so the cache coherence protocol will not act, and so when Thread B reads the value of x, it will read an old value.
My question is: is there anything that prevents the CPU from waiting a long time (for example: 10 minutes) before executing its scheduled set operations?
Note: I know that I can use a memory barrier to force the CPU to execute its scheduled set operations immediately, but I am curious to know what can happen if I don't use a memory barrier.
volatilefor that variable (x), so that it won't cache the value either the thread A or B - ntshetty