How to get rid of a thread in C #

I used streams in my web application, which I 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 works fine and gives me the desired result.

Do I need to kill / destroy the stream after this, if so, how? Please guide.

I said that if I didn’t destroy it, it could cause performance problems.

+6
source share
2 answers

The Join() call is what the thread allocates. You do not have to do anything. Just make sure that threads clean up all the resources that they can use before they exit.

However, I highly recommend that you study the thread pool or the parallel task library (TPL) instead of explicitly managing the threads. They are easier to use and handle such things much smoother.

+11
source

If I were in your place, I would use ThreadPool instead of manual threads. It handles all these things itself, and you do not have the overhead of creating and destroying threads. The code will probably be a bit more complicated because you will need to use ManualResetEvent instead of just Thread.Join () (see How can I execute ThreadPool.Join? ), But you don’t have to worry about creating too many threads, and that’s almost 40 times faster.

In this class for the test, I wrote a comparison of 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 is the output for five runs:

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

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

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

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

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

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

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

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

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

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

+7
source

Source: https://habr.com/ru/post/900089/


All Articles