[英]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 演习是为了
I have a csv textfile with ~400 measuring points from 5 imaginary sensors. 我有一个csv文本文件,包含5个虚拟传感器的约400个测量点。
What I think I have to do: 我认为我必须做的事情:
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: 这里有几个环境依赖:
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.