comparison MonoPlay/Program.cs @ 145:706fccb85524 v2

RC: cancellation support for promises + tests
author cin
date Sun, 08 Mar 2015 02:52:27 +0300
parents f75cfa58e3d4
children 3258399cba83
comparison
equal deleted inserted replaced
144:8c0b95069066 145:706fccb85524
6 using System.Collections.Concurrent; 6 using System.Collections.Concurrent;
7 using System.Threading; 7 using System.Threading;
8 8
9 namespace MonoPlay { 9 namespace MonoPlay {
10 class MainClass { 10 class MainClass {
11
12
11 public static void Main(string[] args) { 13 public static void Main(string[] args) {
12 if (args == null) 14 if (args == null)
13 throw new ArgumentNullException("args"); 15 throw new ArgumentNullException("args");
14 16
15 var t1 = Environment.TickCount; 17 var t1 = Environment.TickCount;
16 18
17 const int reads = 100000; 19 for (int i = 0; i < 10000000; i++) {
18 const int writes = 1000; 20
19 const int readThreads = 8; 21 var p = new Promise<int>();
20 const int writeThreads = 0; 22 p.On(HandleResult);
21 23 p.Resolve(i);
22 var l = new SharedLock(); 24 }
23 var st = new HashSet<int>();
24
25 Action reader1 = () => {
26 for (int i =0; i < reads; i++) {
27 try {
28 l.LockShared();
29 st.Contains(i % 1000);
30 Thread.Sleep(0);
31 } finally {
32 l.Release();
33 }
34 }
35 };
36
37 Action reader2 = () => {
38 for(var i = 0; i < reads; i++)
39 lock(st) {
40 st.Contains(i % 1000);
41 Thread.Sleep(0);
42 }
43 };
44
45 Action writer1 = () => {
46 var rnd = new Random(Environment.TickCount);
47 for (int i = 0; i < writes; i++) {
48 try {
49 l.LockExclusive();
50 st.Add(rnd.Next(1000));
51 //Thread.Sleep(1);
52 } finally {
53 l.Release();
54 }
55 }
56 };
57
58 Action writer2 = () => {
59 var rnd = new Random(Environment.TickCount);
60 for (int i = 0; i < writes; i++) {
61 lock (st) {
62 st.Add(rnd.Next(1000));
63 //Thread.Sleep(1);
64 }
65 }
66 };
67
68
69
70 var readers = new IPromise[readThreads];
71 for (int i = 0; i < readThreads; i++)
72 readers[i] = AsyncPool.RunThread(reader2);
73
74 var writers = new IPromise[writeThreads];
75 for (int i = 0; i < writeThreads; i++)
76 writers[i] = AsyncPool.RunThread(writer1);
77
78
79 new [] {
80 readers.Bundle().On(() => Console.WriteLine("readers complete in {0} ms", Environment.TickCount - t1)),
81 writers.Bundle().On(() => Console.WriteLine("writers complete in {0} ms", Environment.TickCount - t1))
82 }.Bundle().Join();
83
84
85 25
86 var t2 = Environment.TickCount; 26 var t2 = Environment.TickCount;
87 Console.WriteLine("done: {0} ms, {1:.00} Mb, {2} GC", t2 - t1, GC.GetTotalMemory(false) / (1024*1024), GC.CollectionCount(0) ); 27 Console.WriteLine("done: {0} ms, {1:.00} Mb, {2} GC", t2 - t1, GC.GetTotalMemory(false) / (1024*1024), GC.CollectionCount(0) );
88 28
89 } 29 }
90 30
31 static void HandleAction ()
32 {
33
34 }
91 35
36 static void HandleResult(int x) {
37
38 }
92 } 39 }
93 } 40 }