All pastes #319507 Raw Edit

Anonymous

public text v1 · immutable
#319507 ·published 2007-01-17 13:09 UTC
rendered paste body
	class Program
	{
		static void Main(string[] args)
		{
			Semaphore s = new Semaphore(0, int.MaxValue);
			Thread t1 = new Thread(new ParameterizedThreadStart(Thread1));
			Thread t2 = new Thread(new ParameterizedThreadStart(Thread2));
			t1.Start(s);
			t2.Start(s);
			t1.Join();
			t2.Join();
		}

		public static void Thread1(object o)
		{
			Semaphore s = (Semaphore)o;
			s.Release(1);
//			s.Release(1);
		}

		public static void Thread2(object o)
		{
			Semaphore s = (Semaphore)o;
			System.Console.WriteLine("2: waiting 2s");
			Thread.Sleep(2000);
			System.Console.WriteLine("2: 1st wait");
			s.WaitOne();
			System.Console.WriteLine("2: 2nd wait");
			s.WaitOne();
			System.Console.WriteLine("2: done");
		}
}