CLR Thread Object

I got into a debate with a fellow worker about what will happen to a thread if we set it to null after all the work delegated to that thread is finished. So something on the lines of:

Thread go = new Thread();
try {
go.Start()
// Do Work
}
catch { }
finally { go = null; }

As per my understanding, setting the thread to null doesn't specifically "kill" that thread; the GC may be able to free up the space used by that thread object, but what about the actual memory space, stack, etc. that has been assigned to the thread by the Windows OS Because I thought that you can't technically kill a Windows thread without raising the ThreadAbortException Can someone help answer these questions

Thanks in advance.


Answer this question

CLR Thread Object

  • diver182

    Hi,

    The thread will die by natural causes in your example, remember that you must keep it alive feeding it with instructions. When you mean "kill" a thread actually is aborting a thread that still has instructions to run.

    In your example, the "go" object is a reference type as you well know and because is in your method the "go" reference will be destroyed when the method ends, therefore adding "go = null;" is a "waste of energy" as the stack jump will do the job for you. The main object will still live on the heap until the GC collects and put it on the finalization queue. Once the finalization is run the object is actually killed.

    Regarding your question about killing it, based in what I have explained you if you have a thread that has plenty of instructions to run and you assign null to the "reference" the thread will continue running until it finishes or the GC terminates it.

    You can try it:

    {
    Thread YourLongThread = new Thread(new ThreadStart(Test));
    YourLongThread.Start();
    YourLongThread.IsBackground = false;
    YourLongThread = null;
    }

    private void Test(object state)
    {
    Thread.Sleep(20000);
    }

    You will see that the thread continues running after the null assigment.

    Hope this helps you.



  • NoEgo

    Great posts, guys. I am just pleased that my understanding of threads and stack-space is correct! Thanks for confirming!
  • mbp

    Assigning null a Thread variable merely assigns null to that reference to the object. That allows the GC to know you don't need that object anymore in that context. A reference to it while it is running will remain (so, you'd be going from 2 to 1 reference). The GC won't do anything with it until all references are released (e.g. when the thread exits, in your circumstance).

    It's not wise to "kill" a thread, if could be modifying a complex invariant (e.g. writing data to a file) that could get corrupted if you externally terminate a thread. The best way is to write a thread routine that polls a synchronization object that another thread can "set" to signal the thread to exit on its own. See 'System.Threading.Thread.Suspend()' is obsolete: 'Thread.Suspend has been deprecated... for an example of writing a thread routine that allows for cooperative terminate (and suspension).

  • CLR Thread Object