The most effective way to use and communicate with many threads

I am encoding an application that runs many threads in the background, which should report this to the main thread so that it can update the table in the interface. In the past, workflows were the usual separate classes (named Citizen) that I ran from the main thread using something like

new Thread(new ThreadStart(citizen.ProcessActions)).Start(); 

where the ProcessActions function was the main function that did all the background work. Before starting a thread, I would register event handlers so that Citizen threads can write / report some things to the interface. Usually there are dozens of these Citizen streams (about 50), and they are quite large classes - each of them has its own HTTP client and browses the Internet.

Is this a good way to manage threads? Probably not, to be honest; I am sure that the threads do not exit gracefully - as soon as the ProcessActions function is completed, I delete the event handlers, and this - memory usage continues to grow with each new Citizen.

What would be the best way to manage the many (50+) threads that you often have to communicate with? . I suggest that I don’t have to worry about thread safety for Citizen variables as I would not access them from threads other than my own thread.

+4
source share
1 answer

I think you are looking for a thread pool. Here's an MSDN article on them and should be available in C # 4.0.

The idea would be to create a thread pool, determine its number to some large number (say 50), and then start assigning threads to tasks. If the pool needs to expand, it can, but by declaring a large number of fronts, you will get all the expensive creation of threads aside.

It may be useful to complete the "queue" tasks that you want to complete and assign these tasks as threads become available.

In addition, memory leaks can be difficult to find, but I would start by testing a simple case: pull out all the threads (just run one Citizen after another from the main thread) and let it work for a long time. If it is still losing memory, flow control is not a problem.

+2
source

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


All Articles