如何编写利用多核处理器的 .NET 应用程序
随着多核处理器变得越来越流行,我认为熟悉如何编写代码来利用它们是明智的。 我是一名 .NET 开发人员,并且不确定从哪里开始(说真的,我不知道从哪里开始)。 有什么建议么?
As multi-core processors become more and more popular, I think it would be wise for me to become familiar with how to write code to take advantage of them. I am a .NET developer, and am not sure where to begin (seriously, I have no clue where to start). Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
正如 JaredPar 所说,并行扩展非常好。 我玩了一下它们,它们很容易使用并且增加了性能。
我非常喜欢的构造是 Parallel.ForEach() 和 PLinq。
Parallel.ForEach 类似于 DotNet 2.0 集合中的 ForEach(delegate) 函数。 它需要一个操作委托并并行运行它。
Plinq 允许您通过在查询末尾添加 .AsParallel() 来以并行方式执行任何普通的 Linq 查询。
查看 Jared 发布的链接以获取更多信息。
Like JaredPar said the Parallel Extensions are pretty good. I played with them a bit and they are easy to use and add performance.
The constructs I liked a lot were Parallel.ForEach() and PLinq.
Parallel.ForEach is similar to the ForEach(delegate) function that you have in DotNet 2.0 collections. It takes an action delegate and runs it in parallel.
Plinq allows you to execute any normal Linq query in a parallel fashion by adding .AsParallel() to the end of the query.
Check out the links Jared posted for more info.
这可能是一个良好的起点。
This might make a good starting place.
只需将您的项目分成专门用于执行特定功能的各种线程,您就可以利用多个核心。 操作系统负责将不同的线程调度到最可用的处理器核心。
Simply separating your project into various threads specialized to do a particular function will let you take advantage of multiple cores. The OS handles scheduling the different threads to the most available processor core.
多核编程本质上就是多线程编程。 只要您的应用程序是多线程的,操作系统就会确定在哪个核心上运行哪个线程。
那么,您真正的问题应该是,什么样的应用程序会从多线程中受益? 考虑这样一个应用程序,然后实例化多个线程来解决问题的同等部分。
Multi-core programming is essentially multi-threaded programming. As long as your application is multi-threaded, the operating system will determine which core to run which thread on.
So then, your real question should be, what kind of applications would benefit from being multi-threaded? Think of such an application, then instantiate multiple threads to solve equal portions of the problem.
最好的开始方法是获取并行扩展框架的最新 CTP,并开始研究其示例代码。
The best way to start is to grab the latest CTP of the parrallel extensions framework and start plowing through their sample code.
阅读 .NET 使用的异步编程模型。 研究允许委托在线程池中执行的 BeginXXX 和 EnxXXX 范例。
Stream 类都有BeingXXX、EndXXX 方法(例如BeginRead、EndRead),允许您执行异步IO。 如果您正在从事大量基于 IO 的工作,您将需要使用这些方法来提高并行性。
Read up on the asynchronous programming model that .NET uses. Study the BeginXXX and EnxXXX paradigm that allows delegates to execute in the threadpool.
The Stream classes all have BeingXXX, EndXXX methods (eg BeginRead, EndRead) that allow you to do asynchronous IO. If you're working on something that heavily IO based you'll want to use these methods to increase parallelism.
我不再经常推荐书籍,但是 Joe Duffy 的 Windows 上的并发编程 简直是不可替代的。
I don't recommend books very often anymore, but Joe Duffy's Concurrent Programming on Windows is simply irreplaceable.
查找有关 .net 4.0 的信息。 它将为其添加不同的构造。
Look for info on the .net 4.0. It will be adding different constructs for it.