93
|
1 using System;
|
|
2 using Implab.Diagnostics;
|
|
3 using Implab.Parallels;
|
|
4 using Implab;
|
103
|
5 using System.Collections.Generic;
|
|
6 using System.Collections.Concurrent;
|
136
|
7 using System.Threading;
|
93
|
8
|
|
9 namespace MonoPlay {
|
|
10 class MainClass {
|
|
11 public static void Main(string[] args) {
|
94
|
12 if (args == null)
|
|
13 throw new ArgumentNullException("args");
|
|
14
|
103
|
15 var t1 = Environment.TickCount;
|
93
|
16
|
136
|
17 const int reads = 100000;
|
|
18 const int writes = 1000;
|
|
19 const int readThreads = 8;
|
|
20 const int writeThreads = 0;
|
|
21
|
|
22 var l = new SharedLock();
|
|
23 var st = new HashSet<int>();
|
125
|
24
|
136
|
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 };
|
93
|
44
|
136
|
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 };
|
125
|
57
|
136
|
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
|
125
|
69
|
136
|
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
|
121
|
85
|
103
|
86 var t2 = Environment.TickCount;
|
125
|
87 Console.WriteLine("done: {0} ms, {1:.00} Mb, {2} GC", t2 - t1, GC.GetTotalMemory(false) / (1024*1024), GC.CollectionCount(0) );
|
93
|
88
|
|
89 }
|
136
|
90
|
|
91
|
93
|
92 }
|
|
93 }
|