Created
May 18, 2015 13:09
-
-
Save masaeedu/fb1b5065b9df84004dd8 to your computer and use it in GitHub Desktop.
Concurrency problem with no parallelism
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.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