/
monitor-stress.cs
60 lines (58 loc) · 1.23 KB
/
monitor-stress.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Threading;
class T {
static int count = 20000;
static int loops = 80;
static int threads = 10;
static object global_obj;
static void stress_loop () {
object obj = new object ();
lock (obj) {
object [] array = new object [count];
for (int i = 0; i < count; ++i) {
array [i] = new object ();
}
for (int i = 0; i < count; ++i) {
lock (array [i]) {
global_obj = new String ('x', 32);
if ((i % 12) == 0) {
array [i] = global_obj;
}
}
}
// again, after a GC
GC.Collect ();
for (int i = 0; i < count; ++i) {
lock (array [i]) {
}
}
// two times, with feeling
for (int i = 0; i < count; ++i) {
lock (array [i]) {
for (int j = 0; i < count; ++i) {
lock (array [j]) {
}
}
}
}
}
}
static void worker () {
for (int i = 0; i < loops; ++i)
stress_loop ();
}
static void Main (string[] args) {
if (args.Length > 0)
loops = int.Parse (args [0]);
if (args.Length > 1)
count = int.Parse (args [1]);
if (args.Length > 1)
threads = int.Parse (args [2]);
for (int i = 0; i < threads; ++i) {
Thread t = new Thread (new ThreadStart (worker));
t.Start ();
}
/* for good measure */
worker ();
}
}