简体   繁体   English

如何开始创建多线程负载均衡器?

[英]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. 创建一个多线程负载平衡器,每秒从5个传感器读取1个测量点。 (therefore 5 values every second). (因此每秒5个值)。
  • 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) (如传感器1-5的最大值或平均值等,当然是多线程的)
  • 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). 作为一项额外任务,我还必须确保如果将来每秒都会读取500个传感器,计算机就不会退出工作。(负载平衡)。

I have a csv textfile with ~400 measuring points from 5 imaginary sensors. 我有一个csv文本文件,包含5个虚拟传感器的约400个测量点。

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. 我是c#中多​​线程应用程序的新手,但我认为使用threadpool是正确的方法。 I am currently working on a queue and maybe starting it inside a task so it wont block the application. 我目前正在编写一个队列,可能在任务中启动它,因此它不会阻止应用程序。

What would you recommend? 你会推荐什么?

There are a couple of environment dependencies here: 这里有几个环境依赖:

  • What version of .NET are you using? 您使用的是什么版本的.NET?
  • What UI are you using - desktop (WPF/WinForms) or ASP.NET? 您使用的是什么UI - 桌面(WPF / WinForms)还是ASP.NET?

Let's assume that it's .NET 4.0 or higher and a desktop app. 我们假设它是.NET 4.0或更高版本以及桌面应用程序。

Reading the sensors 读传感器

In a WPF or WinForms application, I would use a single BackgroundWorker to read data from the sensors. 在WPF或WinForms应用程序中,我将使用单个BackgroundWorker从传感器读取数据。 500 reads per second is trivial - even 500,00 is usually trivial. 每秒500次读取是微不足道的 - 甚至500,00通常是微不足道的。 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. BackgroundWorker类型专门用于与桌面应用程序交互,例如将结果传递给UI而不必担心线程交互。

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. 如果我们假设它们是短暂的(比如每个不到1秒),那么我认为使用TaskScheduler和标准的ThreadPool就可以了。 So you create a Task for each calculation, and then let the TaskScheduler take care of allocating tasks to threads. 因此,您为每个计算创建一个Task ,然后让TaskScheduler负责将任务分配给线程。

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. TaskScheduler的工作是通过将轻量级任务排队到更重量级的线程来负载平衡工作,并管理ThreadPool以最好地平衡工作负载与计算机上的核心数量。 You can even override the default TaskScheduler to schedule tasks in whatever manner you want. 您甚至可以覆盖默认的TaskScheduler ,以任何方式安排任务。

The ThreadPool is a FIFO queue of work items that need to be processed. ThreadPool是需要处理的工作项的FIFO队列。 In .NET 4.0, the ThreadPool has improved performance by making the work queue a thread-safe ConcurrentQueue collection. 在.NET 4.0中,ThreadPool通过使工作队列成为线程安全的ConcurrentQueue集合来提高性能。

Measuring task throughput and efficiency 测量任务吞吐量和效率

You can use PerformanceCounter to measure both CPU and memory usage . 您可以使用PerformanceCounter来测量CPU和内存使用情况 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 :-) 请注意,我没有在此处包含任何代码,因为我假设您要处理教授的实施细节:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM