Skip to content

Instantly share code, notes, and snippets.

@masaeedu
Created May 18, 2015 13:09
Show Gist options
  • Save masaeedu/fb1b5065b9df84004dd8 to your computer and use it in GitHub Desktop.
Save masaeedu/fb1b5065b9df84004dd8 to your computer and use it in GitHub Desktop.
Concurrency problem with no parallelism
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var sch = new ConcurrentExclusiveSchedulerPair().ExclusiveScheduler;
var tf = new TaskFactory(sch);
var task = tf.StartNew(Main).Result;
task.Wait();
}
static async Task Main()
{
var t1 = NonThreadSafe();
var t2 = NonThreadSafe();
await t1;
await t2;
}
static int SomeNumber = 0;
static async Task NonThreadSafe()
{
var sn = SomeNumber;
SomeNumber += 1;
await Task.Yield();
Debug.Assert(SomeNumber == sn + 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment