Esempio n. 1
0
 public static void latchRelease(Latch latch)
 {
     Thread.Sleep(2000); // Wait two seconds before releasing the latch
     latch.Release();
 }
Esempio n. 2
0
 static void Main(string[] args)
 {
     Console.WriteLine("Enter the utility class you would like to test:\n" +
         "OPTIONS:\n" +
       	"\tsemaphore\n" +
       	"\tchannel\n" +
       	"\tboundedchannel\n" +
       	"\tlightswitch\n" +
       				"\tlatch\n" +
      	"\trwlock\n" +
       	"\tmutex\n" +
         "\tFIFOSemaphore\n" +
         "\tExchanger");
     Console.Write("> ");
     String command = Console.ReadLine();
     command = command.ToLower();
     if (command.Length > 0)
     {
         switch (command)
         {
             case "semaphore":
                 ConcurrencyUtils.Semaphore testSemaphore = new ConcurrencyUtils.Semaphore(0);
                 List<Thread> threads = new List<Thread>();
                 for (int i = 0; i < TEST_THREADS; i++)
                 {
                     Thread newThread = new Thread(() => SemaphoreTest(testSemaphore));
                     newThread.Name = i.ToString();
                     // Console.WriteLine(newThread.Name + " created");
                     newThread.Start();
                     threads.Add(newThread);
                 }
                 Thread releaserThread = new Thread(() => SemaphoreTestReleaser(testSemaphore));
                 releaserThread.Name = "R";
                 releaserThread.Start();
                 break;
             case "mutex":
                 ConcurrencyUtils.Mutex testMutex = new ConcurrencyUtils.Mutex();
                 List<Thread> mutexThreads = new List<Thread>();
                 for (int i = 0; i < TEST_THREADS; i++)
                 {
                     Thread newThread = new Thread(() => SemaphoreTest(testMutex));
                     newThread.Name = i.ToString();
                     newThread.Start();
                     mutexThreads.Add(newThread);
                 }
                 Thread mutexReleaserThread = new Thread(() => MutexTestReleaser(testMutex));
                 mutexReleaserThread.Name = "R";
                 mutexReleaserThread.Start();
                 break;
             case "channel":
                 Channel<String> testChannel = new Channel<String>();
                 List<Thread> grabberThreads = new List<Thread>();
                 Thread putterThread = new Thread(() => putData(testChannel));
                 putterThread.Name = "Putter";
                 putterThread.Start();
                 for (int i = 0; i < TEST_THREADS; i++)
                 {
                     Thread newThread = new Thread(() => grabData(testChannel));
                     newThread.Name = "Thread" + i;
                     Console.WriteLine(newThread.Name + " created");
                     newThread.Start();
                     grabberThreads.Add(newThread);
                 }
                 break;
             case "boundedchannel":
                 BoundChannel<String> testBoundChannel = new BoundChannel<String>(CHANNEL_SIZE);
                 List<Thread> putterThreads = new List<Thread>();
                 Thread grabberThread = new Thread(() => grabData(testBoundChannel));
                 grabberThread.Name = "Grabber";
                 grabberThread.Start();
                 for (int i = 0; i < TEST_THREADS; i++)
                 {
                     Thread newThread = new Thread(() => putData(testBoundChannel));
                     newThread.Name = "Thread" + i;
                     Console.WriteLine(newThread.Name + " created");
                     newThread.Start();
                     putterThreads.Add(newThread);
                 }
                 break;
             case "lightswitch":
                 ConcurrencyUtils.Semaphore semaphore = new ConcurrencyUtils.Semaphore(1);
                 LightSwitch writeSwitch = new LightSwitch(semaphore);
                 List<Thread> writerThreads = new List<Thread>();
                 Thread lonelyThread = new Thread(() => lonelyThreadWrite( semaphore ) );
                 lonelyThread.Start();
                 for (int i = 0; i < 10; i++)
                 {
                     Thread newThread = new Thread(() => groupThreadWrite(writeSwitch));
                     newThread.Name = "Thread" + i;
                     newThread.Start();
                     writerThreads.Add(newThread);
                 }
                 break;
             case "latch":
                 Latch latch = new Latch();
                 List<Thread> waiters = new List<Thread>();
                 for (int i = 0; i < 100; i++)
                 {
                     Thread newThread = new Thread(() => latchAcquire(latch));
                     newThread.Name = "Thread" + i;
                     newThread.Start();
                 }
                 Thread unlocker = new Thread(() => latchRelease(latch));
                 unlocker.Start();
                 break;
             case "rwlock":
                 ConcurrencyUtils.ReaderWriterLock RWLock = new ConcurrencyUtils.ReaderWriterLock();
                 for (int i = 0; i < 10; i++)
                 {
                     Thread newThread = new Thread(() => readLots(RWLock));
                     newThread.Name = "Reader Thread" + i;
                     newThread.Start();
                 }
                 for (int i = 0; i < 2; i++)
                 {
                     Thread newThread = new Thread(() => writeLots(RWLock));
                     newThread.Name = "Writer Thread" + i;
                     newThread.Start();
                 }
                 break;
             case "fifosemaphore":
                 FIFOSemaphore fifoSemaphore = new FIFOSemaphore(1);
                 for (int i = 0; i < 4; i++)
                 {
                     Thread t = new Thread(() => acquireWaitRelease(fifoSemaphore));
                     t.Name = "Thread" + i;
                     t.Start();
                     Thread.Sleep(1000);
                 }
                 break;
             case "exchanger":
                 Exchanger<String> exchanger = new Exchanger<String>();
                 String string1 = "String 1";
                 String string2 = "String 2";
                 Thread thread1 = new Thread(() => exchangeString(exchanger, string1));
                 Thread thread2 = new Thread(() => exchangeString(exchanger, string2));
                 thread1.Name = "Thread 1";
                 thread2.Name = "Thread 2";
                 thread1.Start();
                 thread2.Start();
                 thread1.Join();
                 thread2.Join();
                 break;
             default:
                 Console.WriteLine("Test for '" + args[0] + "' not implemented");
                 break;
         }
     }
     else
     {
         Console.WriteLine("No argument provided");
     }
 }