我怎样才能获得“可用”的计数?我的应用程序的处理器?

发布于 2024-08-19 19:23:09 字数 401 浏览 2 评论 0原文

我知道如何获取计算机上的物理处理器数量和逻辑处理器数量,但我想知道我的应用程序可以访问多少个逻辑处理器。

例如,我在四核机器上进行开发,但我有许多单核用户,并且在许多情况下我“简化”了界面,或者遇到了多核系统从未遇到过的锁定问题。

因此,为此,我设置了 VSTS 以在调试或“调试单核”中构建我的应用程序。目标基本上是将处理器亲和力设置为核心“0”,通过查看 Windows 任务管理器是否按预期工作。

我的问题是,我刚刚注意到(事后看来这应该是显而易见的),在我的整个代码中,我有 Environment.ProcessorCount >= some,这对于真正的单核机器非常有用,但没有让我了解我的单个“逻辑上可用的核心”。

如何获取“可用”逻辑核心的数量?

C# 优先

I know how to get the number of physical, and number of logical processors on my machine, but I want to know how many logical processors my application has access to.

For instance, I develop on a quad core machine, but I have a number of single core users out there, and in many instances I "dumb down" the interface, or run into locking problems that a multi-core system never experiences.

So, to that end I have set up VSTS to build my app in either debug or "Debug Single Core". The goal there is basically to set the processor affinity to core "0", which by looking at the windows task manager is working as expected.

My problem is, I just noticed, ( and hind sight says it should have been obvious ), that throughout my code I have Environment.ProcessorCount >= something, which works great for truly single core machines, but doesn't give me a read on my single "logically available core".

How can I get the number of "available" logical cores?

C# preferred

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

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

发布评论

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

评论(1

但可醉心 2024-08-26 19:23:09

特别感谢Jesse Slicer找到的答案此处

虽然不是所问问题的公认答案,但这是我正在寻找的答案。

这基本上是我根据杰西的回答得到的结果。

#if !DEBUG
                return Environment.ProcessorCount;
#endif
                using (Process currentProcess = Process.GetCurrentProcess())
                {
                    var processAffinityMask =
                        (uint) currentProcess.ProcessorAffinity;
                    const uint BitsPerByte = 8;
                    var loop = BitsPerByte*sizeof (uint);
                    uint result = 0;

                    while (loop > 0)
                    {
                        --loop;
                        result += (processAffinityMask & 1);
                        processAffinityMask >>= 1;
                    }

                    return Convert.ToInt32((result != 0) ? result : 1);
                }

A special thanks goes out to Jesse Slicer's answer found here.

Although not the accepted answer to the question asked, it IS the answer I am looking for.

Here is basicly what I ended up with based on Jesse's answer.

#if !DEBUG
                return Environment.ProcessorCount;
#endif
                using (Process currentProcess = Process.GetCurrentProcess())
                {
                    var processAffinityMask =
                        (uint) currentProcess.ProcessorAffinity;
                    const uint BitsPerByte = 8;
                    var loop = BitsPerByte*sizeof (uint);
                    uint result = 0;

                    while (loop > 0)
                    {
                        --loop;
                        result += (processAffinityMask & 1);
                        processAffinityMask >>= 1;
                    }

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