Mercurial > pub > ImplabNet
comparison MonoPlay/Program.cs @ 136:e9e7940c7d98 v2
shared locks + tests
author | cin |
---|---|
date | Mon, 16 Feb 2015 01:14:09 +0300 |
parents | f803565868a4 |
children | f75cfa58e3d4 |
comparison
equal
deleted
inserted
replaced
135:656815cb7147 | 136:e9e7940c7d98 |
---|---|
2 using Implab.Diagnostics; | 2 using Implab.Diagnostics; |
3 using Implab.Parallels; | 3 using Implab.Parallels; |
4 using Implab; | 4 using Implab; |
5 using System.Collections.Generic; | 5 using System.Collections.Generic; |
6 using System.Collections.Concurrent; | 6 using System.Collections.Concurrent; |
7 using System.Threading; | |
7 | 8 |
8 namespace MonoPlay { | 9 namespace MonoPlay { |
9 class MainClass { | 10 class MainClass { |
10 public static void Main(string[] args) { | 11 public static void Main(string[] args) { |
11 if (args == null) | 12 if (args == null) |
12 throw new ArgumentNullException("args"); | 13 throw new ArgumentNullException("args"); |
13 | 14 |
14 const int count = 10000000; | |
15 | |
16 var t1 = Environment.TickCount; | 15 var t1 = Environment.TickCount; |
17 | 16 |
18 for (int i = 0; i < count; i++) { | 17 const int reads = 100000; |
19 var p = new Promise<int>(); | 18 const int writes = 1000; |
19 const int readThreads = 8; | |
20 const int writeThreads = 0; | |
20 | 21 |
21 p.On(x => {}).On(x => {}); | 22 var l = new SharedLock(); |
23 var st = new HashSet<int>(); | |
22 | 24 |
23 p.Resolve(i); | 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 }; | |
24 | 36 |
25 } | 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 }; | |
26 | 44 |
27 | 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(reader1); | |
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 | |
28 | 85 |
29 var t2 = Environment.TickCount; | 86 var t2 = Environment.TickCount; |
30 Console.WriteLine("done: {0} ms, {1:.00} Mb, {2} GC", t2 - t1, GC.GetTotalMemory(false) / (1024*1024), GC.CollectionCount(0) ); | 87 Console.WriteLine("done: {0} ms, {1:.00} Mb, {2} GC", t2 - t1, GC.GetTotalMemory(false) / (1024*1024), GC.CollectionCount(0) ); |
31 | 88 |
32 } | 89 } |
90 | |
91 | |
33 } | 92 } |
34 } | 93 } |