-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestMutex.cs
More file actions
88 lines (74 loc) · 2.67 KB
/
TestMutex.cs
File metadata and controls
88 lines (74 loc) · 2.67 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using ConcurrencyUtilities;
using Mutex = ConcurrencyUtilities.Mutex;
using System.Collections.Generic;
using System.Threading;
namespace TestConcurrencyUtilities
{
public class TestMutex
{
public static Mutex _accessToBankAccount;
public static int _magnitude;
public static int _balance;
public static void ChangeBalanceBy(int delta) {
_accessToBankAccount.Acquire();
_balance = _balance + delta;
TestSupport.DebugThread("Balance: $" + _balance);
_accessToBankAccount.Release();
}
public static void Incrementer() {
for (int i = 0; i < _magnitude; i++)
ChangeBalanceBy(1);
}
public static void Decrementer() {
for (int i = 0; i < _magnitude; i++)
ChangeBalanceBy(-1);
}
public static void Run(int magnitude) {
_balance = 0;
_magnitude = magnitude;
_accessToBankAccount = new Mutex();
TestSupport.Log(ConsoleColor.Blue, "Mutex test\n==============================");
TestSupport.Log(ConsoleColor.Blue, "\nTesting mutual exclusion\n---------------------");
TestSupport.Log(ConsoleColor.Blue, "\nA mutex will be used to modify a bank account balance by positive " +
"and negative $" + _magnitude + " in steps of $1. The resulting balance should be $0.");
TestSupport.SleepThread(4000);
List<Thread> threads = new List<Thread>();
threads.AddRange( TestSupport.CreateThreads(Incrementer, "Incrementer", 1) );
threads.AddRange( TestSupport.CreateThreads(Decrementer, "Decrementer", 1) );
TestSupport.RunThreads(threads);
TestSupport.Log(ConsoleColor.Blue, "\nTesting release methods\n---------------------");
Mutex m = new Mutex();
TestSupport.Log(ConsoleColor.Blue, "\nTesting Release():");
Console.WriteLine(" Acquire()...");
m.Acquire();
Console.WriteLine(" Release()...");
m.Release();
TestSupport.Log(ConsoleColor.Blue, "\nTesting Release(1):");
Console.WriteLine(" Acquire()...");
m.Acquire();
Console.WriteLine(" Release(1)...");
m.Release(1);
TestSupport.Log(ConsoleColor.Blue, "\nTesting Release(2):");
Console.WriteLine(" Acquire()...");
m.Acquire();
try {
Console.WriteLine(" Release(2) (should throw exception):");
m.Release(2);
} catch {
Console.WriteLine(" Exception thrown successfully for Release(2)");
}
TestSupport.Log(ConsoleColor.Blue, "\nTesting Release() * 2:");
Console.WriteLine(" Mutex still has 0 tokens");
try {
Console.WriteLine(" Release()...");
m.Release();
Console.WriteLine(" Release() (should throw exception)...");
m.Release();
} catch {
Console.WriteLine(" Exception thrown successfully for extra Release()");
}
Console.WriteLine("\nFinished");
}
}
}