如何设置程序的CPU亲和力?

发布于 07-25 10:03 字数 300 浏览 13 评论 0原文

我有一个用 C# 编写的程序,我使用 VSTS 2008 + .Net 3.5 + Windows Vista Enterprise x86 来开发 Windows 窗体应用程序。

我当前的计算机是双核CPU,我想将程序的CPU亲和力设置为在特定CPU上运行,并释放另一个CPU来执行其他工作。 有什么想法如何做到这一点? 通过编码或配置都可以。

多一点背景是,我的程序是CPU密集型的,所以我不想让它占用我计算机上的所有两个CPU资源,我想释放一个CPU,以便我可以同时快速浏览网络。 :-)

提前致谢, 乔治

I have a program written in C#, I am using VSTS 2008 + .Net 3.5 + Windows Vista Enterprise x86 to develop a Windows Forms application.

My current computer is dual-core CPU, I want to set CPU affinity of my program to run on a specific CPU and free another CPU to do some other job. Any ideas how to do this? Either through coding or configuration is ok.

A little more background is, my program is CPU intensive, so I do not want to let it occupy all two CPU resources on my computer and I want to free one CPU so that I can browse network at the same time quickly. :-)

thanks in advance,
George

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放飞的风筝2024-08-01 10:03:09
  1. 转到任务管理器 -> 流程选项卡。
  2. 寻找你的程序。 右键单击它。
  3. 选择设置关联性并取消选中其中一个复选框。

这应该可以为您释放一个处理器。

要从代码中执行此操作,您可以添加以下语句:

System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = (System.IntPtr) 1;

干杯!

  1. Go to Task Manager -> Processes tab.
  2. Look for your program. Right click on it.
  3. Select Set Affinity and uncheck one of the checkboxes.

This should free up one processor for you.

For doing it from the code you can add this statement:

System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = (System.IntPtr) 1;

Cheers!

烟酉2024-08-01 10:03:09

执行此操作的 Windows API 函数为 SetProcessAffinityMask() SetThreadAffinityMask( )。 我不了解.NET,所以我不能说这些函数是否有包装器,但是 似乎表明并非如此。

顺便说一句:我同意这些仅在非常特殊的情况下才是必要的,通常最好让操作系统调度程序来处理它。 如果你必须问如何做,那么这是你可能不应该做的问题之一。

The Windows API functions to do this are SetProcessAffinityMask() and SetThreadAffinityMask(). I don't know .NET so I can't say whether there are wrappers around those functions, but this seems to suggest otherwise.

BTW: I agree that these are necessary only in very specific circumstances, it's normally best to let the OS scheduler deal with it. It's one of those questions where you probably shouldn't do it if you have to ask how.

夏尔2024-08-01 10:03:09

实际上,您的应用程序不会使用多个 CPU,除非您专门采取一些措施来利用更多 CPU。 如果您使用线程池和/或启动额外的线程,您可以使用额外的可用核心,但否则您的应用程序默认只有一个线程,因此只使用一个 CPU。

Actually, your application will not use more than one CPU unless you specifically do something to utilize more CPUs. If you use the thread pool and/or start additional threads you may use additional available cores, but otherwise your application will just have one thread per default and thus only use one CPU.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文