我怎样才能得到与QUOT计数;可用"我的应用处理器? [英] How can I get a count of "Available" processors for my application?

查看:106
本文介绍了我怎样才能得到与QUOT计数;可用"我的应用处理器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何让身体的数量,和我的机器上的逻辑处理器数量,但我​​想知道有多少个逻辑处理器我的应用程序可以访问。

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.

所以,为此我已成立了VSTS建立我在调试或调试单核的应用程序。我们的目标有基本设置处理器亲和力核心0,它通过查看Windows任务管理器工作正常。

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.

我的问题是,我只注意到,(和后视说,它应该是显而易见的),那我整个code我有 Environment.ProcessorCount> =东西,它的伟大工程,为真正的单核机,但不给我读我的单曲在逻辑上可用的核心。

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

C# preferred

推荐答案

一个特别要感谢的是杰西切片机的找到答案<一href="http://stackoverflow.com/questions/188503/detecting-the-number-of-processors/189371#189371">here.

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.

下面是basicly什么我结束了基于杰西的回答。

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);
                }

这篇关于我怎样才能得到与QUOT计数;可用&QUOT;我的应用处理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆