.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Ties Between a Thread Object and an Operating System Thread
 

Ties Between a Thread Object and an Operating System Thread

Hello All,

Please consider the code below. The "thread" object becomes garbage nearly immediately after it's created. What happens to the corresponding operating system thread when the "thread" object gets garbage collected? Also, please notice the question embedded in comment form in the code below.

Thanks,
Dave

void Foo()
{
Thread thread = new Thread(new ThreadStart(EntryPoint));
thread.IsBackground = true; // Does the answer to my question depend on how I set this?
}

void EntryPoint()
{
while(true)
{
// Block while waiting for a work item.

//Process the work item we just received.
}
}
dtheese

The thread class is simply a wrapper around an Operating system thread object. Not it doesn't implement IDisposable, this is because the OS handles the termination and cleanup of the OS thread object when it receives notification that a process has terminated.

The corresponding operating system thread continues to execute even if the Thread object is collected. If the thread is set to background, however, and the application terminates, the thread that was set to background will also terminate along with the process. If the thread is not a background thread, the OS will allow the process to continue running until the thread terminates.

Take the following small program for example:

static void Main( string[] args )
{
Thread t = new Thread(() =>
{
Thread.Sleep(2000);
Console.WriteLine("Hi");
Console.ReadLine();
});
t.IsBackground = true;
t.Start();
}

This thread never finishes executing before the process terminates, and the background thread with it. Comment out that line, and the application will wait until the thread completes to terminate the application.

That being said, you can't be sure when the Thread object is collected. There's nothing in this application indicating that the Thread object will be collected immediately. It might be out fo scope, but that doesn't mean it's collected. It first has to go through the same garbage collection algorithm as everything else. Out of scope doesn't necessarily mean collected.

But to answer your question, IsBackground has no effect on whether or not something is garbage collected, or when it is. It's completely independent of that.


To simplify:

The OS thread doesn't terminate just because the Thread object is collected.
The thread will continue to execute until it terminates if IsBackground is set to false. If IsBackground is set to true, and all the foreground threads in the application terminate, the thread in question will be brought down along with the last foreground thread, unless it completes before all foreground threads are terminated, in which case, it will execute to completion.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki �LinkedIn �ForumsBrowser
  • Marked As Answer bydtheese Tuesday, September 22, 2009 9:13 PM
  •  
David M Morton

The thread class is simply a wrapper around an Operating system thread object. Not it doesn't implement IDisposable, this is because the OS handles the termination and cleanup of the OS thread object when it receives notification that a process has terminated.

The corresponding operating system thread continues to execute even if the Thread object is collected. If the thread is set to background, however, and the application terminates, the thread that was set to background will also terminate along with the process. If the thread is not a background thread, the OS will allow the process to continue running until the thread terminates.

Take the following small program for example:

static void Main( string[] args )
{
Thread t = new Thread(() =>
{
Thread.Sleep(2000);
Console.WriteLine("Hi");
Console.ReadLine();
});
t.IsBackground = true;
t.Start();
}

This thread never finishes executing before the process terminates, and the background thread with it. Comment out that line, and the application will wait until the thread completes to terminate the application.

That being said, you can't be sure when the Thread object is collected. There's nothing in this application indicating that the Thread object will be collected immediately. It might be out fo scope, but that doesn't mean it's collected. It first has to go through the same garbage collection algorithm as everything else. Out of scope doesn't necessarily mean collected.

But to answer your question, IsBackground has no effect on whether or not something is garbage collected, or when it is. It's completely independent of that.


To simplify:

The OS thread doesn't terminate just because the Thread object is collected.
The thread will continue to execute until it terminates if IsBackground is set to false. If IsBackground is set to true, and all the foreground threads in the application terminate, the thread in question will be brought down along with the last foreground thread, unless it completes before all foreground threads are terminated, in which case, it will execute to completion.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki �LinkedIn �ForumsBrowser
  • Marked As Answer bydtheese Tuesday, September 22, 2009 9:13 PM
  •  
David M Morton

You can use google to search for other answers

Custom Search

More Threads

• Array and Array list and Hastable
• Does the "System.Diagnostics.PerformanceCounter" Class give the information about Application source
• Why does RSACryptoServiceProvider require delegation?
• Custom stream that replaces substrings
• Single .NET executable - linkers
• Can't xml serialize type of [,]
• Rounding to/away from zero .05
• ASP Webparts
• AES 128 using SslStream Class in .Net framework 2.0
• Trial Period in vb.net appln.