Created
August 7, 2018 17:48
-
-
Save retran/fa8c6b6671f0c91091a986a22d0f528b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
namespace MemoryReordering | |
{ | |
class Program | |
{ | |
private static int x; | |
private static int y; | |
private static int r1; | |
private static int r2; | |
private static void Foo() | |
{ | |
x = 1; | |
r1 = y; | |
} | |
private static void Bar() | |
{ | |
y = 1; | |
r2 = x; | |
} | |
static void Main(string[] args) | |
{ | |
for (int i = 0; i < 10000; i++) | |
{ | |
x = 0; | |
y = 0; | |
r1 = 0; | |
r2 = 0; | |
var t1 = Task.Run(() => Foo()); | |
var t2 = Task.Run(() => Bar()); | |
Task.WaitAll(t1, t2); | |
if (r1 == 0 && r2 == 0) | |
{ | |
throw new InvalidOperationException("WTF???"); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@NoofSaeidh It will never fail if you start it only at one core :)