-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestExchanger.cs
57 lines (48 loc) · 1.97 KB
/
TestExchanger.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
using System;
using ConcurrencyUtilities;
using Semaphore = ConcurrencyUtilities.Semaphore;
using Mutex = ConcurrencyUtilities.Mutex;
using System.Collections.Generic;
using System.Threading;
using AnsiColor;
namespace TestConcurrencyUtilities
{
public class TestExchanger
{
static int _sleepTime;
static int _magnitude;
static Exchanger<string> _exchanger;
static void AttendExchange() {
string intelToGive = "I-" + TestSupport.ThreadName();
TestSupport.DebugThread("{yellow}Tx:"+intelToGive);
string intelReceived = _exchanger.Arrive(intelToGive);
if (intelReceived != intelToGive)
TestSupport.DebugThread("{green}Rx:"+intelReceived);
else
TestSupport.DebugThread("ERROR:Rx:"+intelReceived);
}
static void AttendExchangeAfterDelay() {
TestSupport.SleepThread(_sleepTime);
AttendExchange();
}
public static void Run(int magnitude, int sleepTime) {
_magnitude = magnitude;
_sleepTime = sleepTime;
_exchanger = new Exchanger<string>();
TestSupport.Log(ConsoleColor.Blue, "Exchanger test\n==============================");
TestSupport.Log(ConsoleColor.Blue, "\nTwo secret agent threads will arrive at an exchange, " +
"waiting for the other to arrive before parting ways (rendezvous).\n");
TestSupport.SleepThread(_sleepTime);
List<Thread> threads = new List<Thread>();
threads.AddRange( TestSupport.CreateThread(AttendExchange, "Agent A") );
threads.AddRange( TestSupport.CreateThread(AttendExchangeAfterDelay, "Agent B") );
TestSupport.RunThreads(threads);
TestSupport.Log(ConsoleColor.Blue, "\nMany secret agent threads will arrive at an exchange, " +
"waiting for the other to arrive before parting ways (rendezvous).\n");
threads = new List<Thread>();
threads.AddRange( TestSupport.CreateThreads(AttendExchange, "A", _magnitude, 0, 7+1, 1) );
TestSupport.EndColumnHeader(_magnitude, 7+1); // End the column header line
TestSupport.RunThreads(threads);
}
}
}