Created
February 16, 2017 12:26
-
-
Save Flash3001/ec0da534167bd3cdb85bcfc12eff0838 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.Linq; | |
using System.Threading; | |
namespace TearDateTime | |
{ | |
class Program | |
{ | |
const int ITERATIONS = 20000; | |
const int MAX_THREADS = 30; | |
static int threads = 1; // 2x | |
static int rerun = 0; | |
static bool found = false; | |
static bool stop = false; | |
static DateTime _value; | |
static DateTime[] _values; | |
static CountdownEvent _threadsEndend; | |
static void Setup() | |
{ | |
var range = Enumerable.Range(0, threads); | |
var random = new Random(); | |
_values = range.Select(c => new DateTime((long)random.Next() << 30 | (long)random.Next())).ToArray(); | |
_threadsEndend = new CountdownEvent(threads * 2); | |
_value = _values[0]; // Remove to get false positives! You will get 'normal' races, not tears. | |
} | |
static void Assign(int i) | |
{ | |
for (int c = 0; c < ITERATIONS; c++) | |
{ | |
_value = _values[i]; | |
} | |
_threadsEndend.Signal(); | |
} | |
static void FindTear() | |
{ | |
for (int c = 0; c < ITERATIONS; c++) | |
{ | |
var date = _value; | |
if (!_values.Contains(date)) | |
{ | |
Console.WriteLine($"Teared: {date}"); | |
found = true; | |
} | |
} | |
_threadsEndend.Signal(); | |
} | |
static unsafe void Run() | |
{ | |
while (!found && !stop) | |
{ | |
Setup(); | |
Console.Clear(); | |
Console.WriteLine("----- Trying to tear ------"); | |
Console.WriteLine($"Running: {sizeof(IntPtr) * 8}bits"); | |
Console.WriteLine($"DateTime: {sizeof(DateTime) * 8}bits"); | |
Console.WriteLine($"Threads: {threads}"); | |
Console.WriteLine($"Rerun: {rerun}"); | |
Console.WriteLine(); | |
Console.WriteLine("---------- Values ----------"); | |
for (int i = 0; i < threads; i++) | |
{ | |
Console.WriteLine($"{i}: {_values[i]}"); | |
} | |
Console.WriteLine(); | |
Console.WriteLine("---------- Tears ----------"); | |
for (int i = 0; i < threads; i++) | |
{ | |
var l = i; | |
new Thread(() => Assign(l)).Start(); | |
new Thread(FindTear).Start(); | |
} | |
_threadsEndend.Wait(); | |
_threadsEndend.Dispose(); | |
threads++; | |
if (threads == MAX_THREADS) | |
{ | |
rerun++; | |
threads = 1; | |
} | |
} | |
} | |
static void Main(string[] args) | |
{ | |
new Thread(Run).Start(); | |
Console.ReadLine(); | |
stop = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment