Skip to content

Instantly share code, notes, and snippets.

@Restuta
Created February 8, 2012 20:03

Revisions

  1. Restuta revised this gist Feb 8, 2012. No changes.
  2. @invalid-email-address Anonymous renamed this gist Feb 8, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @invalid-email-address Anonymous renamed this gist Feb 8, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @invalid-email-address Anonymous created this gist Feb 8, 2012.
    58 changes: 58 additions & 0 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    using System;
    using System.Threading;
    using System.Threading.Tasks;

    namespace ConsoleApplication3
    {
    class ThreadUnsafe
    {
    public static int _val1 = 1;
    public static int _val2 = 1;

    private static readonly object syncObj = new object();

    public void Go()
    {
    lock (syncObj)
    {
    if (_val2 != 0)
    {
    Thread.Sleep(0);
    var i = _val1/_val2;
    }
    }
    _val2 = 0;
    }
    }

    class Program
    {
    static void Main(string[] args)
    {
    ThreadUnsafe threadUnsafe1 = new ThreadUnsafe();
    ThreadUnsafe threadUnsafe2 = new ThreadUnsafe();

    for (int i = 0; i < 10000; i++)
    {
    Task task = new Task(threadUnsafe1.Go);
    Task task1 = new Task(threadUnsafe2.Go);

    task.Start();
    task1.Start();

    try
    {
    Task.WaitAll(task, task1);
    }
    catch (AggregateException ex)
    {
    Console.WriteLine(i);
    Console.WriteLine(ex);
    }

    ThreadUnsafe._val2 = 1;
    }

    }
    }
    }