Re: Monitor weirdness

Russell Stuart <russell-M7Clnf/[email protected]>
Newsgroups gmane.comp.gnu.dotgnu.developer
Message-ID <1083738922.1535.1.camel@ras>
On Wed, 2004-05-05 at 15:49, Thong (Tum) Nguyen wrote: 
> Check this code out:
> 
> using System;
> using System.Threading;
> 
> public class Test
> {
> 	Thread thread1, thread2;
> 	
> 	public void Run1()
> 	{
> 		Thread.Sleep(1000);
> 		
> 		lock (this)
> 		{
> 			Monitor.Pulse(this);
> 			thread2.Interrupt();
> 			
> 			for (;;)
> 			{
> 				Console.WriteLine("Thread 1");
> 			}
> 		}
> 	}
> 	
> 	public void Run2()
> 	{
> 		lock (this)
> 		{
> 			try
> 			{
> 				Monitor.Wait(this);
> 			}
> 			catch (ThreadInterruptedException)
> 			{
> 				Console.WriteLine("2 Interrupted");
> 			}
> 			
> 			for (;;)
> 			{
> 				Console.WriteLine("Thread 2");
> 			}
> 		}
> 	}
> 
> 	public void Go()
> 	{
> 		thread1 = new Thread(new ThreadStart(Run1));
> 		thread2 = new Thread(new ThreadStart(Run2));
> 		
> 		thread1.Start();
> 		thread2.Start();
> 	}
> 		
> 	public static void Main()
> 	{
> 		new Test().Go();
> 	}
> }
> 
> 
> On MS.NET you get a "Thread 1" and "Thread 2" printing out (alternating)
> which makes it looks like both synchronization blocks are running at once
> (which they are!).  Ouch.

I compiled it under VS 2003 and ran it.  Only "Thread 1" was printed
out.  Thread 2 did not exit the Wait() call.  I replaced the the
Interrupt() with Abort(), and got the same result.

Under dotgnu 0.6.4 and the current CVS it ran identically.

Russell - did you run it on a multi CPU machine?
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.