How can I start creating a multi-threaded load balancer?

I have an interesting exercise to solve from my professor. But I need a little help, so I don't get boring during the holidays.

The exercise -

  • Create a multi-threaded load balancer that reads 1 measuring point from 5 sensors every second. (therefore 5 values ​​every second).
  • Then do some “complicated” calculations with these values.
  • Printing the results of calculations on the screen. (for example, the maximum value or the average value of the sensor 1-5, etc., of course, multi-threaded).
  • As an additional task, I also have to make sure that if in the future, for example, 500 sensors are read every second, the computer will not stop working (load balancing).

I have a csv text file with ~ 400 measurement points from 5 imaginary sensors.

I think I need to do:

  • Read the measuring points in the array
  • Provide secure access to this array by stream
  • Create a new stream for each value that calculates some mathematical data.
  • Set maximum value for maximum concurrent workflows

I am new to multithreaded applications in C #, but I think using threadpool is the right way. I am currently working on a queue and maybe running it inside a task, so it will not block the application.

What would you recommend?

+5
source share
1 answer

There are several environment dependencies here:

  • What version of .NET are you using?
  • Which user interface do you use - desktop (WPF / WinForms) or ASP.NET?

Suppose this is .NET 4.0 or higher and a desktop application.

Reading sensors

In a WPF or WinForms application, I would use a single BackgroundWorker to read data from sensors. 500 views per second is trivial - even 500.00 is usually trivial. The BackgroundWorker type is specifically designed for interacting with desktop applications, for example, for outputting results to the user interface without worrying about thread interactions.

Computation processing

Then you need to handle the “complex” calculations. It depends on how durable these calculations are. If we assume that they are short-lived (say, less than 1 second each), then I think using TaskScheduler and the standard ThreadPool will be fine. This way you create a Task for each calculation, and then let the TaskScheduler take care of the distribution of tasks for the threads.

TaskScheduler's task is to balance the load by queuing tasks for heavier threads and managing ThreadPool to better balance the workload and the number of cores on the machine. You can even override the default TaskScheduler to schedule tasks in any way.

ThreadPool is the FIFO queue of work items that need to be processed. In .NET 4.0, ThreadPool has improved performance by creating a work queue in the thread-safe ConcurrentQueue collection .

Performance and efficiency of the measurement task

You can use the PerformanceCounter to measure both CPU and memory usage . This will give you an idea of ​​whether the cores and memory are being used efficiently. Task throughput is simply measured by examining the processing speed of the tasks and presenting the results.

Please note: I have not included any code here, as I assume that you want to deal with the details of your professor's implementation :-)

+3
source

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


All Articles