开发者

How can I get a count of "Available" processors for my application?

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 obvio开发者_StackOverflow社区us ), 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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜