... continued
Continues the discussion on C#'s memory model.
Let's take another common example quoted in online literature that illustrates the effects of reordering operations. Study the code below:
Press + to interact
public class ReorderExample{int myVal;bool done;void threadB(){if (done){Console.WriteLine(myVal);}}void threadA(){myVal = 7;done = true;}}
Say the two threads A and B run the methods threadA()
and threadB()
respectively. Ignoring the memory model we can conclude that there can be one of two outcomes:
Either nothing gets printed on the console if thread B runs before thread A.
7 is printed on the console.
If we take the memory model into account, we can have any of the following outcomes:
Nothing is printed on ...
Access this course and 1400+ top-rated courses and projects.