How to dispose thread in c#
Asked Answered
L

2

7

I have used threading in my web application which I have mentioned below:

var t1 = new Thread(F1);
            t1.IsBackground = true;
            t1.Start();

            var t2 = new Thread(F2);
            t2.IsBackground = true;
            t2.Start();

            var t3 = new Thread(F3);
            t3.IsBackground = true;
            t3.Start();

            var t4 = new Thread(F4);
            t4.IsBackground = true;
            t4.Start();


            t1.Join();
            t2.Join();
            t3.Join();
            t4.Join();

This is working fine and giving me the desired output.

Do I need to kill/Dispose the thread after this, if yes then how ? Please guide.

I have told that if I do not dispose it, it might raise performance issue.

Loupe answered 25/10, 2011 at 22:39 Comment(1)
There is nothing else you should have to doCircumscription
A
14

The call to Join() is what de-allocates the thread. You don't have to do anything else. Just make sure that the threads clean up any resources they might be using before they exit.

That said, I would urge you to look into using the thread pool or the Task Parallel Library (TPL) rather than explicitly managing threads. They're easier to use, and handle this kind of thing much more smoothly.

Aplacental answered 25/10, 2011 at 22:43 Comment(2)
Thanks. I was too worried about it. Now I am confident. Thanks againLoupe
Technically, Join does not deallocate a thread. It just waits for the thread to complete. The GC does the deallocation for you.Satterfield
P
7

If I were in your shoes, I'd be using a ThreadPool instead of manual threads. It handles all that stuff itself, and you don't have the overhead of creating and destroying threads. The code will probably be slightly more complex, because you'll need to use a ManualResetEvent instead of a simple Thread.Join() (see How can I accomplish ThreadPool.Join?), but you won't have to worry about creating too many threads, and it's nearly 40x faster.

Here's the class for a test I wrote comparing the two approaches:

class ThreadPoolVsThreads
{
    private static readonly PerformanceMonitor threadPoolTest = new PerformanceMonitor("ThreadPoolTest");
    private static readonly PerformanceMonitor threadTest = new PerformanceMonitor("ThreadTest");

    private const int iterations = 100;
    private const int threads = 10;
    private static long somevalue;

    public static void Test()
    {
        TestHelper.PerformTest(10, threadPoolTest, ThreadPoolTest);
        TestHelper.PerformTest(10, threadTest, ThreadTest);
    }

    private static void ThreadPoolTest(int iteration)
    {
        for (int i = 0; i < iterations; i++)
        {
            var resetEvents = new ManualResetEvent[threads];
            for (int j = 0; j < threads; j++)
            {
                var re = new ManualResetEvent(false);
                resetEvents[j] = re;
                ThreadPool.QueueUserWorkItem(o =>
                {
                    somevalue++;
                    re.Set();
                });
            }
            WaitHandle.WaitAll(resetEvents);
        }
    }

    private static void ThreadTest(int iteration)
    {
        for (int i = 0; i < iterations; i++)
        {
            var threadArray = new Thread[threads];
            for (int j = 0; j < threads; j++)
            {
                var thread = new Thread(o => somevalue++);
                threadArray[j] = thread;
                thread.Start();
            }
            for (int j = 0; j < threads; j++)
            {
                threadArray[j].Join();
            }
        }
    }
}

And here's the output for five runs:

ThreadPoolTest action completed: iteration = 1, completionTime = 53, averageCompletionTime = 53.000

ThreadTest action completed: iteration = 1, completionTime = 2128, averageCompletionTime = 2128.000

ThreadPoolTest action completed: iteration = 2, completionTime = 42, averageCompletionTime = 47.500

ThreadTest action completed: iteration = 2, completionTime = 2149, averageCompletionTime = 2138.500

ThreadPoolTest action completed: iteration = 3, completionTime = 65, averageCompletionTime = 53.333

ThreadTest action completed: iteration = 3, completionTime = 2078, averageCompletionTime = 2118.333

ThreadPoolTest action completed: iteration = 4, completionTime = 72, averageCompletionTime = 58.000

ThreadTest action completed: iteration = 4, completionTime = 2137, averageCompletionTime = 2123.000

ThreadPoolTest action completed: iteration = 5, completionTime = 43, averageCompletionTime = 55.000

ThreadTest action completed: iteration = 5, completionTime = 2085, averageCompletionTime = 2115.400

Philemol answered 25/10, 2011 at 22:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.