简体   繁体   English

多核硬件哪个更好? 多线程或异步方法

[英]Which is better for multi-core hardware? multithreading or asynchronous methods

We need to develop a dashboard application for a giant screen. 我们需要为大型屏幕开发仪表板应用程序。 What it is does is collect all KPIs(Key Performance Indicators) and shows it on the giant screen in realtime in a visual manner so that the management leads know whats going on. 它的作用是收集所有KPI(关键绩效指标)并将其以可视方式实时显示在大屏幕上,以便管理领导知道发生了什么。

So giant screen will have 20 to 30 independent graphs & pie charts(the number might increase soon) data needs to be refreshed at a configurable amount of time. 因此,巨大的屏幕将具有20到30个独立的图形和饼图(数量可能会很快增加),需要在可配置的时间内刷新数据。 What it technically means is lot of database calls to pull the data and notify the data changes to the GUI. 从技术上讲,它意味着需要进行大量数据库调用才能提取数据并将数据更改通知给GUI。 As there are many individual graphs to update, I do not want to update them synchronously because a time consuming database query of one graph will delay the update of another graph. 由于有许多要更新的单个图,因此我不想同步更新它们,因为一个图的耗时数据库查询会延迟另一个图的更新。

So I have two choices here, 所以我有两个选择

  1. Run the database calls, calculations & notifying the GUI of the changes in data in a separate thread/task for each graph. 在每个图形的单独线程/任务中运行数据库调用,计算并通知GUI数据的变化。
  2. Write all the database calls as asynchronous methods, implement calculations and notifying the GUI in the callbacks 将所有数据库调用编写为异步方法,实现计算并在回调中通知GUI

Though either of the method will suffice the purpose, I want to know which solution is better especially considering the fact the hardware has 16 cores. 尽管这两种方法都可以满足目的,但我想知道哪种解决方案更好,尤其是考虑到硬件具有16个内核的事实。 What are the advantages and disadvantages of both of these methods? 这两种方法的优缺点是什么? or Is there any better approach for this problem? 还是有解决这个问题的更好方法?

We are planning to use .NET 4.0 & WPF for the UI & C# as development language. 我们计划将.NET 4.0和WPF用于UI和C#作为开发语言。

I think the key points about the two techniques are following: 我认为有关这两种技术的要点如下:

  • Multi-threaded approach limits the number of operations you can do in parallel, because threads are relatively expensive resources. 多线程方法限制了您可以并行执行的操作数量,因为线程是相对昂贵的资源。 However if you need something like ~20 threads, you won't have any troubles. 但是,如果您需要约20个线程,则不会有任何麻烦。 It may be easier to write this in C#, because you can use usual sequential programming style. 用C#编写此代码可能会更容易,因为您可以使用通常的顺序编程样式。

  • Asynchronous calls allow you to perform much larger number of I/O bound operations "in parallel", because each call does not occupy an entire thread, so this is more efficient and also benefits from the multiple cores on the system (because asynchronous operations are handled using thread pool). 异步调用使您可以“并行”执行更大数量的I / O绑定操作,因为每个调用都不会占用整个线程,因此这效率更高,并且还受益于系统上的多个内核(因为异步操作是使用线程池处理)。 This may be more difficult to use from C#, because you need to use the APM methods such as BeginXyz (which makes some usual programming patterns hard to encode). 在C#中,这可能更难使用,因为您需要使用诸如BeginXyz之类的APM方法(这使一些常见的编程模式难以编码)。

You can use some advanced libraries for writing asynchronous code in C# - for example AsynchronousEnumerator gives you a relatively comfortable programming style. 您可以使用一些高级库来用C#编写异步代码-例如, AsynchronousEnumerator为您提供了一种相对舒适的编程风格。 You may also consider using F# which has asynchronous workflows and message passing concurrency , which would be quite likely a perfect match for your problem. 您也可以考虑使用具有异步工作流程消息传递并发性的 F#,这很可能是您问题的完美匹配。

I recommend using PLINQ and the task parallel library if possible. 如果可能,我建议使用PLINQ和任务并行库 They include optimum partitioning, thread scaling, and work stealing all built-in. 它们包括最佳分区,线程扩展和窃取所有内置内容的工作。

Each of those methods will use multi-threading to achieve your goal. 这些方法中的每一个都将使用多线程来实现您的目标。

Asynchronous methods just make the heavy lifting portion a little easier (they are asynchronous because they start on another thread and notify the original thread upon completion). 异步方法只会使繁重的工作变得容易一些(它们是异步的,因为它们在另一个线程上启动,并在完成时通知原始线程)。

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

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