[Mono-list] Threading

Sanin Saracevic ssaracevic@interland.com
Mon, 13 May 2002 11:29:42 -0400


The following simple test that was compiled with csc on win2k machine shows
that the main thread waits as long as it takes for the worker thread to
complete.

using System;
using System.Threading;

namespace testMT
{
	public class testMT
	{
		public static void Main()
		{
			Thread _t = new Thread(new ThreadStart(go));
			_t.Start();
			Console.WriteLine("Main thread exiting");
		}
		
		private static void go()
		{
			for (int _i = 1; _i < 1000; _i++)
				Console.WriteLine("thread still alive: {0}",
_i.ToString());
			Console.WriteLine("thread exiting");
		}
	}
}

I find this behavior rather weird and scary since there is nothing in the
generated IL code to show that csc injects an implicit call to Join. It
seems that the runtime keeps the main thread alive while the worker thread
is still running. Can someone offer any reason why this should work this
way?

Sanin Saracevic, MCP
Lead Software Architect

404.720.8301x4562

 -----Original Message-----
From: 	Chris J. Breisch [mailto:cjbreisch@altavista.net] 
Sent:	Monday, May 13, 2002 10:50 AM
To:	'Ian McCullough'; 'Jonathan Stowe'
Cc:	mono-list@ximian.com
Subject:	RE: [Mono-list] Threading

I'm not sure that loading down your machine to "near-death" levels would
help.  You still have the same number of instructions to execute in the
new thread and the same number of instructions to execute in the ending
thread.  In any kind of round-robin (with aging and priority)
task-switching, it seems to me that the behavior in this setup should be
relatively consistent.

So, if csc consistently works as Jonathan expects and mcs does not, it
implies one of four things to me.

1) csc executes fewer instructions than mcs to start a thread
2) csc executes more instructions than mcs to end a process
3) csc temporarily sets the priority of the new thread higher than the
priority of the thread that started it
4) csc does an implicit Join() as Dick stated


I don't see any other possibilities.

			-chris

Chris J. Breisch, MCSD, MCDBA



-----Original Message-----
From: mono-list-admin@ximian.com [mailto:mono-list-admin@ximian.com] On
Behalf Of Ian McCullough
Sent: Monday, May 13, 2002 9:26 AM
To: Jonathan Stowe
Cc: mono-list@ximian.com
Subject: Re: [Mono-list] Threading

> OK. That does what I expected ... except that with csc it does it
without
> the Join() everytime ...

Yeah -- I tested that before I posted my reply, and I couldn't get it to
fail in the same way, however I do think that this behavior is still "by
design" in that even though csc starts the thread in time, that it is
not
guaranteed to do so.  I suspect that if you loaded down your windows
machine
to "near-death" levels, that the thread spawning process might take
enough
time for the main thread to exit before the new thread got to its
Console.WriteLine statement.  The bottom line and the issue at the heart
of
this is that once you start a thread, there are no guarantees whatsoever
about execution order or synchronization unless you specifically put
those
guarantees in there using synchronization devices.

Ian


_______________________________________________
Mono-list maillist  -  Mono-list@ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list



_______________________________________________
Mono-list maillist  -  Mono-list@ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list