Skip to content

Instantly share code, notes, and snippets.

@iamandrewluca
Created February 20, 2015 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamandrewluca/00dca6713b7bc943fb3e to your computer and use it in GitHub Desktop.
Save iamandrewluca/00dca6713b7bc943fb3e to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace pr2
{
class Program
{
static bool useCountDownEvent = true;
static void Main(string[] args)
{
if (useCountDownEvent)
{
Console.WriteLine("Using CountDownEvent:");
new UsingCountDownEvent();
}
else
{
Console.WriteLine("Using Join:");
new UsingJoin();
}
Console.ReadKey();
}
}
class UsingCountDownEvent
{
Thread[] threads = new Thread[9];
CountdownEvent[] counters = new CountdownEvent[4];
public UsingCountDownEvent()
{
for (int i = 0; i < 4; ++i)
{
// 0 - counter for event 4
// 1 - counter for event 5
// 2 - counter for event 6
// 3 - coutner for event 7
counters[i] = new CountdownEvent(3);
}
threads[0] = new Thread(() => event123(1));
threads[1] = new Thread(() => event123(2));
threads[2] = new Thread(() => event123(3));
threads[3] = new Thread(event4);
threads[4] = new Thread(() => event567(5));
threads[5] = new Thread(() => event567(6));
threads[6] = new Thread(() => event567(7));
threads[7] = new Thread(() => event89(8));
threads[8] = new Thread(() => event89(9));
foreach (Thread thread in threads)
{
thread.Start();
}
foreach (Thread thread in threads)
{
if (thread.IsAlive)
{
thread.Join();
}
}
Console.ReadKey();
}
void event123(int n)
{
Console.WriteLine("Message from event " + n);
counters[0].Signal();
}
void event4()
{
counters[0].Wait();
Console.WriteLine("Message from event 4");
signal567();
}
void event567(int n)
{
counters[n - 4].Wait();
Console.WriteLine("Message from event " + n);
}
void event89(int n)
{
Console.WriteLine("Message from event " + n);
signal567();
}
void signal567()
{
counters[1].Signal();
counters[2].Signal();
counters[3].Signal();
}
}
class UsingJoin
{
Thread[] threads = new Thread[9];
public UsingJoin()
{
threads[0] = new Thread(() => event123(1));
threads[1] = new Thread(() => event123(2));
threads[2] = new Thread(() => event123(3));
threads[3] = new Thread(event4);
threads[4] = new Thread(() => event567(5));
threads[5] = new Thread(() => event567(6));
threads[6] = new Thread(() => event567(7));
threads[7] = new Thread(() => event89(8));
threads[8] = new Thread(() => event89(9));
foreach (Thread thread in threads)
{
thread.Start();
}
foreach (Thread thread in threads)
{
if (thread.IsAlive)
{
thread.Join();
}
}
}
void event123(int n)
{
Console.WriteLine("Message from event " + n);
}
void event4()
{
threads[0].Join();
threads[1].Join();
threads[2].Join();
Console.WriteLine("Message from event 4");
}
void event567(int n)
{
threads[3].Join();
threads[7].Join();
threads[8].Join();
Console.WriteLine("Message from event " + n);
}
void event89(int n)
{
Console.WriteLine("Message from event " + n);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment