Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get started creating a multithreaded load balancer?

I have an interesting exercise to solve from my professor. But I need a little bit of help so it does not become boring during the holidays.

The exercise is to

  • create a multithreaded load balancer, that reads 1 measuring point from 5 sensors every second. (therefore 5 values every second).
  • Then do some "complex" calculations with those values.
  • Printing results of the calculations on the screen. (like max value or average value of sensor 1-5 and so on, of course multithreaded)
  • As an additional task I also have to ensure that if in the future for example 500 sensors would be read every second the computer doesn't quit the job.(load balancing).

I have a csv textfile with ~400 measuring points from 5 imaginary sensors.

What I think I have to do:

  1. Read the measuring points into an array
  2. Ensure thread safe access to that array
  3. Spawn a new thread for every value that calculates some math stuff
  4. Set a max value for maximum concurrent working threads

I am new to multithreading applications in c# but I think using threadpool is the right way. I am currently working on a queue and maybe starting it inside a task so it wont block the application.

What would you recommend?

like image 699
michael_j Avatar asked Oct 18 '22 19:10

michael_j


1 Answers

There are a couple of environment dependencies here:

  • What version of .NET are you using?
  • What UI are you using - desktop (WPF/WinForms) or ASP.NET?

Let's assume that it's .NET 4.0 or higher and a desktop app.

Reading the sensors

In a WPF or WinForms application, I would use a single BackgroundWorker to read data from the sensors. 500 reads per second is trivial - even 500,00 is usually trivial. And the BackgroundWorker type is specifically designed for interacting with desktop apps, for example handing-off results to the UI without worrying about thread interactions.

Processing the calculations

Then you need to process the "complex" calculations. This depends on how long-lived these calculations are. If we assume they're short-lived (say less than 1 second each), then I think using the TaskScheduler and the standard ThreadPool will be fine. So you create a Task for each calculation, and then let the TaskScheduler take care of allocating tasks to threads.

The job of the TaskScheduler is to load-balance the work by queuing lightweight tasks to more heavyweight threads, and managing the ThreadPool to best balance the workload vs the number of cores on the machine. You can even override the default TaskScheduler to schedule tasks in whatever manner you want.

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

Measuring task throughput and efficiency

You can use PerformanceCounter to measure both CPU and memory usage. This will give you a good idea of whether the cores and memory are being used efficiently. The task throughput is simply measured by looking at the rate at which tasks are being processed and supplying results.

Note that I haven't included any code here, as I assume you want to deal with the implementation details for your professor :-)

like image 151
HTTP 410 Avatar answered Oct 21 '22 16:10

HTTP 410