Program.cs
93 lines
| 2.8 KiB
| text/x-csharp
|
CSharpLexer
/ MonoPlay / Program.cs
cin
|
r93 | using System; | ||
using Implab.Diagnostics; | ||||
using Implab.Parallels; | ||||
using Implab; | ||||
cin
|
r103 | using System.Collections.Generic; | ||
using System.Collections.Concurrent; | ||||
cin
|
r136 | using System.Threading; | ||
cin
|
r93 | |||
namespace MonoPlay { | ||||
class MainClass { | ||||
public static void Main(string[] args) { | ||||
cin
|
r94 | if (args == null) | ||
throw new ArgumentNullException("args"); | ||||
cin
|
r103 | var t1 = Environment.TickCount; | ||
cin
|
r93 | |||
cin
|
r136 | const int reads = 100000; | ||
const int writes = 1000; | ||||
const int readThreads = 8; | ||||
const int writeThreads = 0; | ||||
var l = new SharedLock(); | ||||
var st = new HashSet<int>(); | ||||
cin
|
r125 | |||
cin
|
r136 | Action reader1 = () => { | ||
for (int i =0; i < reads; i++) { | ||||
try { | ||||
l.LockShared(); | ||||
st.Contains(i % 1000); | ||||
Thread.Sleep(0); | ||||
} finally { | ||||
l.Release(); | ||||
} | ||||
} | ||||
}; | ||||
Action reader2 = () => { | ||||
for(var i = 0; i < reads; i++) | ||||
lock(st) { | ||||
st.Contains(i % 1000); | ||||
Thread.Sleep(0); | ||||
} | ||||
}; | ||||
cin
|
r93 | |||
cin
|
r136 | Action writer1 = () => { | ||
var rnd = new Random(Environment.TickCount); | ||||
for (int i = 0; i < writes; i++) { | ||||
try { | ||||
l.LockExclusive(); | ||||
st.Add(rnd.Next(1000)); | ||||
//Thread.Sleep(1); | ||||
} finally { | ||||
l.Release(); | ||||
} | ||||
} | ||||
}; | ||||
cin
|
r125 | |||
cin
|
r136 | Action writer2 = () => { | ||
var rnd = new Random(Environment.TickCount); | ||||
for (int i = 0; i < writes; i++) { | ||||
lock (st) { | ||||
st.Add(rnd.Next(1000)); | ||||
//Thread.Sleep(1); | ||||
} | ||||
} | ||||
}; | ||||
cin
|
r125 | |||
cin
|
r136 | var readers = new IPromise[readThreads]; | ||
for (int i = 0; i < readThreads; i++) | ||||
readers[i] = AsyncPool.RunThread(reader1); | ||||
var writers = new IPromise[writeThreads]; | ||||
for (int i = 0; i < writeThreads; i++) | ||||
writers[i] = AsyncPool.RunThread(writer1); | ||||
new [] { | ||||
readers.Bundle().On(() => Console.WriteLine("readers complete in {0} ms", Environment.TickCount - t1)), | ||||
writers.Bundle().On(() => Console.WriteLine("writers complete in {0} ms", Environment.TickCount - t1)) | ||||
}.Bundle().Join(); | ||||
cin
|
r121 | |||
cin
|
r103 | var t2 = Environment.TickCount; | ||
cin
|
r125 | Console.WriteLine("done: {0} ms, {1:.00} Mb, {2} GC", t2 - t1, GC.GetTotalMemory(false) / (1024*1024), GC.CollectionCount(0) ); | ||
cin
|
r93 | |||
} | ||||
cin
|
r136 | |||
cin
|
r93 | } | ||
} | ||||