I keep reading code and examples of multi-threaded applications. From time to time I see a snippet like so:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("started");
Go();
Console.WriteLine("press [ENTER] to quit");
Console.ReadLine();
}
private static void Go()
{
var thread = new Thread(DoSomething);
thread.Start();
}
private static void DoSomething()
{
Console.WriteLine("doing something");
}
}
And it keeps bothering me: should I keep the reference to the (foreground) thread somewhere? After all, it's a local variable inside Go
method. Hence, once the execution of Go
is completed, the thread reference should be garbage-collected. So maybe, just maybe, the thread will be GCed while it's executing?
Will the answer change if it's a background thread?
Thanks in advance!
To wait for a thread use the std::thread::join() function. This function makes the current thread wait until the thread identified by *this has finished executing. A C++ program is given below.
Thread Termination After finish their work, threads terminate. In the quicksort example, after both array subsegments are sorted, the threads created for sorting them terminate. In fact, the thread that creates these two child threads terminates too, because its assigned task completes.
Thread management is done in user space by the thread library. When thread makes a blocking system call, the entire process will be blocked. Only one thread can access the Kernel at a time, so multiple threads are unable to run in parallel on multiprocessors.
A thread is one example of an object whose lifetime is not controlled by the garbage collector. Under the hood it is an operating system object. That's alive as long as a thread is running code. The Thread class is just a wrapper for it. Another example is a window, it is alive as long as your code or the user doesn't close it. Winforms doesn't require that you hold a reference to the Form class wrapper. And you very typically don't:
Application.Run(new Form1());
is the boiler plate code, you don't hold a reference to the Form1 class instance anywhere.
You can always re-create a Thread object from an existing running thread. You do so with Thread.CurrentThread. And it doesn't have to be a thread that you created with the Thread constructor. It can be used inside a threadpool thread. Or a thread that wasn't started by managed code. The main thread of your program would be a good example of that, it was started by Windows.
Nevertheless, losing a reference to a thread is not good practice. It implies that you can't check that it is still running. Which implies that you cannot stop it when it should be stopped. When the user wants to quit your program for example.
According to this answer, you have nothing to worry about. The GC is smart enough to know if a thread is being used.
Here is an article explaining the behavior: http://msdn.microsoft.com/en-us/magazine/bb985011.aspx#ctl00_MTContentSelector1_mainContentContainer_ctl03
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With