From time to time I will write a program to remind myself of something, and here I wrote yesterday when something strange happened.
Source source
using System;
using System.Threading;
namespace TestThreadFunctions
{
class Program
{
static void Main(string[] args)
{
Thread.CurrentThread.Name = "Main thread";
var t = JoiningAnAlreadyCompletedThread();
JoiningIsIdempotent(t);
Console.ReadKey();
}
static Thread JoiningAnAlreadyCompletedThread()
{
Thread t = new Thread(() =>
{
Console.WriteLine($"{Thread.CurrentThread.Name} ({Thread.CurrentThread.ManagedThreadId}): I'm done now.\n");
})
{ Name = "Worker 1" };
t.Start();
t.Join(10000);
Console.WriteLine($"{Thread.CurrentThread.Name} ({Thread.CurrentThread.ManagedThreadId}): I just joined on another thread that I knew was already completed even before I joined. See nothing happened. .NET doesn't punish you for joining a thread that has already completed, unlike POSIX systems, which do.\n");
return t;
}
static void JoiningIsIdempotent(Thread threadToJoinOn)
{
Console.WriteLine($"{Thread.CurrentThread.Name} ({Thread.CurrentThread.ManagedThreadId}): I am about to join a thread that I already joined with earlier. I want to see if joining is idempotent.\n");
threadToJoinOn.Join();
Console.WriteLine($"{Thread.CurrentThread.Name} ({Thread.CurrentThread.ManagedThreadId}): I just finished joining with a thread that I already joined with earlier. See! nothing happened. Joining is idempotent.\n");
}
}
}
When I looked at the program in Reflector, it had this strange condition ifat the beginning of the method JoiningAnAlreadyCompletedThread, for example:
private static Thread JoiningAnAlreadyCompletedThread()
{
if (<>c.<>9__1_0 == null)
{
ThreadStart start1 = <>c.<>9__1_0;
}
...
}
I read a couple of times to confirm that I did not miss anything. It is so strange that it does nothing.
For completeness only, here is a fully decompiled program from Reflector.
Decompiled Reflector Source
internal class Program
{
private static Thread JoiningAnAlreadyCompletedThread()
{
if (<>c.<>9__1_0 == null)
{
ThreadStart start1 = <>c.<>9__1_0;
}
Thread thread1 = new Thread(<>c.<>9__1_0 = new ThreadStart(<>c.<>9.<JoiningAnAlreadyCompletedThread>b__1_0)) {
Name = "Worker 1"
};
Thread t = thread1;
t.Start();
t.Join(0x2710);
Console.WriteLine(string.Format("{0} ({1}): I just joined on another thread that I knew was already completed even before I joined. See nothing happened. .NET doesn't punish you for joining a thread that has already completed, unlike POSIX systems, which do.\n", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId));
return t;
}
private static void JoiningIsIdempotent(Thread threadToJoinOn)
{
Console.WriteLine(string.Format("{0} ({1}): I am about to join a thread that I already joined with earlier. I want to see if joining is idempotent.\n", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId));
threadToJoinOn.Join();
Console.WriteLine(string.Format("{0} ({1}): I just finished joining with a thread that I already joined with earlier. See! nothing happened. Joining is idempotent.\n", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId));
}
private static void Main(string[] args)
{
Thread.CurrentThread.Name = "Main thread";
JoiningIsIdempotent(JoiningAnAlreadyCompletedThread());
Console.ReadKey();
}
[Serializable, CompilerGenerated]
private sealed class <>c
{
public static readonly Program.<>c <>9 = new Program.<>c();
public static ThreadStart <>9__1_0;
internal void <JoiningAnAlreadyCompletedThread>b__1_0()
{
Console.WriteLine(string.Format("{0} ({1}): I'm done now.\n", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId));
}
}
}
I searched the same source in IL Spy and didn't do this weird. It did not have this strange condition ifthat did nothing.
IL Spy Decompiled
private static Thread JoiningAnAlreadyCompletedThread()
{
ThreadStart arg_20_0;
if ((arg_20_0 = Program.<>c.<>9__1_0) == null)
{
arg_20_0 = (Program.<>c.<>9__1_0 = new ThreadStart(Program.<>c.<>9.<JoiningAnAlreadyCompletedThread>b__1_0));
}
Thread thread = new Thread(arg_20_0)
{
Name = "Worker 1"
};
thread.Start();
thread.Join(10000);
Console.WriteLine(string.Format("{0} ({1}): I just joined on another thread that I knew was already completed even before I joined. See nothing happened. .NET doesn't punish you for joining a thread that has already completed, unlike POSIX systems, which do.\n", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId));
return thread;
}
So, I think, I don’t know what the question is. Maybe "why did the Reflector do this?"
: ", ", .
, , , , .
, , , Reflector .
, , .