If the process contains only 1 thread. And in the code we define a variable int x = 10
, it's not qualified by volatile
.
- first, thread runs on cpu core1, it read
x
and change it to5
; - then, thread sleep and re-scheduled onto another cpu core2;
- then, thread try to read
x
from memory;
I have several questions:
Q1: cpu contains store buffer, and thread task_struct contains register values, so even though the store buffer not flushed to cache, the thread still will read the latest value by restoring the task context?
Q2: If the cpu register is reused by other instructions following changing x to
5
, latest value5
isn't stored in register now. Now switching happens, thread runs on another core2 now, will the thread read the latest value? Because the store buffer isn't flushed, cpu core2 won't see the cacheline invalidated, I think the thread will read from memory, so read value is10
? Or the OS apply some barriers for flushing out the entire store buffer before context switching and invalidating the invalidate queue before reading?