C# 应用程序如何在 .NET 或 Java 中使用多个内核或 CPU?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37089/
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 09:44:18  来源:igfitidea点击:

How can an application use multiple cores or CPUs in .NET or Java?

提问by Luke Foust

When launching a thread or a process in .NET or Java, is there a way to choose which processor or core it is launched on? How does the shared memory model work in such cases?

在 .NET 或 Java 中启动线程或进程时,有没有办法选择启动它的处理器或内核?在这种情况下,共享内存模型如何工作?

采纳答案by Corey

If you're using multiple threads, the operating system will automatically take care of using multiple cores.

如果您使用多线程,操作系统会自动处理多核的使用。

回答by Alexandru Nedelcu

The operating system takes care of multi-threading when the virtual machine is using native threads (as opposed to green-threads), and you can't specify low level details, like choosing a processor for a certain thread. It is better that way because you usually have many more threads than you have processors available, so the operating system needs to do time-slicing to give all threads a chance to run.

当虚拟机使用本机线程(而不是绿色线程)时,操作系统会处理多线程,并且您无法指定低级细节,例如为某个线程选择处理器。这样做更好,因为您通常拥有比可用处理器多得多的线程,因此操作系统需要进行时间分片以让所有线程都有机会运行。

That being said, you can set threads priorities if you have a critical task, and a threading API usually provides this possibility. See the Java API for example: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#setPriority(int)

话虽如此,如果您有一个关键任务,您可以设置线程优先级,线程 API 通常提供这种可能性。例如,请参阅 Java API:http: //java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#setPriority(int)

PS: there's something broken in the parsing engine ... I had to add the above link as plain text

PS:解析引擎出了点问题......我不得不将上面的链接添加为纯文本

回答by Orion Edwards

is there a way to choose which processor or core it is launched on?

有没有办法选择它在哪个处理器或核心上启动?

You can use the task manager to tell windows what CPU(s) your program should be allowed to run on. Normally this is only useful for troubleshooting legacy programs which have broken implementations of multi-threading. To do this,

您可以使用任务管理器告诉 Windows 应该允许您的程序在哪些 CPU 上运行。通常,这仅用于对已破坏多线程实现的遗留程序进行故障排除。去做这个,

  • Run task manager
  • Find your process in the Processeswindow.
  • Right click and choose Set Affinity...
  • Tick the checkboxes next to the CPU's you want to allow your application to run on. Windows will then only schedule threads from that process onto those particular CPU's
  • 运行任务管理器
  • Processes窗口中找到您的进程。
  • 右键单击并选择 Set Affinity...
  • 勾选要允许应用程序运行的 CPU 旁边的复选框。然后,Windows 只会将该进程中的线程调度到那些特定 CPU 上

If I recall correctly, windows will 'remember' these settings for subsequent times your process is run, but please don't quote me on that - run some tests yourself :-)

如果我没记错的话,Windows 会“记住”这些设置,以便您的进程在后续运行时使用,但请不要引用我的话 - 自己运行一些测试:-​​)

You can also do this programatically in .NET after your program has launched using using the System.Diagnostics.Process.ProcessorAffinityproperty, but I don't think it will 'remember' the settings, so there will always be a short period in which your app is run on whichever CPU windows sees fit. I don't know how to do this in java sorry.

在使用System.Diagnostics.Process.ProcessorAffinity属性启动程序后,您也可以在 .NET 中以编程方式执行此操作,但我认为它不会“记住”设置,因此总会有很短的时间您的应用程序在任何 CPU 窗口上运行。抱歉,我不知道如何在 Java 中执行此操作。

Note:

笔记:

This applies at the entire process level. If you set affinity for CPU0 only, and then launch 50 threads, all 50 of those threads will run on CPU0, and CPU1, 2, 3, etc will sit around doing nothing.

这适用于整个过程级别。如果您只为 CPU0 设置关联,然后启动 50 个线程,那么所有 50 个线程都将在 CPU0 上运行,而 CPU1、2、3 等将无所事事。

Just to reiterate the point, this is primarily useful for troubleshooting broken legacy software. If your software is not broken, you really shouldn't mess with any of these settings, and let windows decide the best CPU(s) to run your program on, so it can take the rest of the system's performance into account.

重申这一点,这主要用于对损坏的遗留软件进行故障排除。如果您的软件没有损坏,您真的不应该弄乱这些设置中的任何一个,并让 Windows 决定运行程序的最佳 CPU,以便它可以考虑系统的其余性能。



As for the 'shared memory' model, it works the same, but there are more things that can go subtly wrong when your app is running on multiple CPU's as opposed to just timeslices on a single one.

至于“共享内存”模型,它的工作原理是相同的,但是当您的应用程序在多个 CPU 上运行而不是仅在单个 CPU 上运行时间片时,可能会出现更多细微错误。

For an eye-opening example, read this ridiculousfish article about CPU's and Memory Barriers.

对于一个令人大开眼界的例子,请阅读这篇关于 CPU 和内存屏障的可笑的文章

It's aimed at OSX development on PowerPC, but general enough that it should apply everywhere. IMHO it's one of the top ten 'all developers should read this' articles I've read.

它针对 PowerPC 上的 OSX 开发,但足够通用,应该适用于任何地方。恕我直言,这是我读过的十大“所有开发人员都应该阅读这篇”文章之一。

回答by sieben

I have used this in a couple of programs because my core 0 was kind of messed up.

我在几个程序中使用过它,因为我的核心 0 有点搞砸了。

// Programmatically set process affinity
var process = System.Diagnostics.Process.GetCurrentProcess();

// Set Core 0
process.ProcessorAffinity = new IntPtr(0x0001);

or

或者

// Set Core 1
process.ProcessorAffinity = new IntPtr(0x0002);

More on this in "Process.ProcessorAffinity Property".

在“ Process.ProcessorAffinity Property”中有更多关于此的信息。

回答by Mark Harris

I would have a look at the Parallel extensions to the .NET framework. It is still in CTP, however it supposed to make the best use of multi core processors. The easiest place to get started for .NET is on the parallel teams blog.

我会看看 .NET 框架的 Parallel 扩展。它仍然在CTP 中,但它应该充分利用多核处理器。开始使用 .NET 最简单的地方是在并行团队博客上

As for Java I have no idea.

至于Java,我不知道。