显式多核 C# 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/190349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:22:27  来源:igfitidea点击:

Explicit multi-core C# applications

c#multicoreruntime-environment

提问by TK.

Is there any easyway to explicitly make an already threaded application utilise a multicore environment? (if avaliable).

是否有任何简单的方法可以显式地使已经线程化的应用程序利用多核环境?(如果有的话)。

The first problem would be how do I determine if my runtime environment supports multi-core applications?

第一个问题是如何确定我的运行时环境是否支持多核应用程序?

If I then determine that I can use multi core functionality, can I explicitly assign threads to run on different cores? If so Is the thread communication handled by the OS/Hardware or do I have to get my hands dirty?

如果我确定我可以使用多核功能,我可以明确分配线程在不同的核上运行吗?如果是这样,线程通信是由操作系统/硬件处理还是我必须弄脏手?

采纳答案by mattlant

If your application is already threaded properly, you dont have to do anything. Thread context execution and handling is done for you.

如果您的应用程序已正确线程化,则您无需执行任何操作。线程上下文执行和处理已为您完成。

Now when it comes to threading, is your app threaded in a way to utilize the cores? do you have processor intensive tasks split into seperate threads. If not, then you will need to make changes to split up your processing tasks into more threads. You can use Environment.ProcessorCount to get the core count and make as many threads as appropriate at runtime for your tasks.

现在谈到线程,您的应用程序是否以利用内核的方式进行线程处理?您是否将处理器密集型任务拆分为单独的线程。如果没有,那么您将需要进行更改以将处理任务拆分为更多线程。您可以使用 Environment.ProcessorCount 获取核心计数并在运行时为您的任务创建尽可能多的线程。

As of now, there is not really any way to make an improperly threaded app(single thread) into a threaded app taking advantage of all the processors/cores, unless you look to future technologies, such as plinqand Parallel FX

到目前为止,没有任何方法可以将不正确的线程应用程序(单线程)变成利用所有处理器/内核的线程应用程序,除非您着眼于未来的技术,例如plinqParallel FX

回答by Nick

Without getting into specifics (I'm not sure how much information is public yet), the next version of the .NET framework will be more helpful with doing this. You will likely not want or need to be thinking about managing threads by hand. I'm sure more information will be public about this after PDC (end of October).

没有进入细节(我不确定有多少信息是公开的),下一个版本的 .NET 框架将在这方面更有帮助。您可能不想或不需要考虑手动管理线程。我相信在 PDC(10 月底)之后会有更多信息公开。

回答by mattlant

Hi there are 2 extentions that I know about

嗨,我知道有 2 个扩展

Parallel C# Homepage

并行 C# 主页

Check out Somasegars MSDN blog on MS June CTP for parallel 3.5 extenstions

在 MS June CTP 上查看 Somasegars MSDN 博客,了解并行 3.5 扩展

MSDN

MSDN

The library is made up of the following components:

该库由以下组件组成:

  • The Task Parallel Library (TPL), which provides support for imperative data and task parallelism.
  • Parallel LINQ (PLINQ), which provides support for declarative data parallelism.
  • Coordination Data Structures (CDS), which provide support for work coordination and managing shared state.
  • 任务并行库 (TPL),它为命令式数据和任务并行性提供支持。
  • Parallel LINQ (PLINQ),它为声明式数据并行性提供支持。
  • 协调数据结构 (CDS),为工作协调和管理共享状态提供支持。

回答by Matt J

Currently, you can set the available processors for a given processusing Process.ProcessorAffinity, but assigning specific threads to specific processors is handled under the hood implicitly. This is actually a good thing most of the time, as an intelligent runtime using real-time performance and load balance information can usually do a better job than you can statically, at least for the few-core context mainstream multicore operates in.

目前,您可以使用Process.ProcessorAffinity为给定进程设置可用处理器,但将特定线程分配给特定处理器是隐式处理的。这在大多数情况下实际上是一件好事,因为使用实时性能和负载平衡信息的智能运行时通常可以比静态运行时做得更好,至少对于主流多核运行的少核上下文。

回答by Matt J

That's the problem. How to make a threaded application, how to convert your existing code. I want to exploit multi-core in C#. However, ParallelFX is still beta. I won't develop using a beta. And, I don't want to change all the code again (I translated my apps from VB 6). Today, I received a newsletter from Packt Publishing announcing a new book about parallel programming with C#. C# 2008 and 2005 Threaded Programming. I think it focuses in the things we are blogging about everywhere. How to think and develop a threaded application without waiting for a new miracle language. It's cool to see books entirely dedicated about the topic, because we have full of blogs about that. The link is http://www.packtpub.com/beginners-guide-for-C-sharp-2008-and-2005-threaded-programming/bookI think it deserves an apportunity. I'll let you know my feedback when I buy it.

那就是问题所在。如何制作线程应用程序,如何转换现有代码。我想在 C# 中利用多核。但是,ParallelFX 仍处于测试阶段。我不会使用测试版进行开发。而且,我不想再次更改所有代码(我从 VB 6 翻译了我的应用程序)。今天,我收到了 Packt Publishing 的时事通讯,其中宣布了一本关于使用 C# 进行并行编程的新书。C# 2008 和 2005 线程编程。我认为它的重点是我们到处都在写博客的事情。如何在不等待新的奇迹语言的情况下思考和开发线程应用程序。看到完全专注于该主题的书籍很酷,因为我们有很多关于该主题的博客。链接是http://www.packtpub.com/beginners-guide-for-C-sharp-2008-and-2005-threaded-programming/book我认为它值得一个机会。当我购买时,我会告诉你我的反馈。