Click here to Skip to main content
13,292,205 members (57,294 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 1 Feb 2005

Counting Physical and Logical Processors

, 1 Feb 2005
Rate this:
Please Sign up or sign in to vote.
How to determine the number of physical and logical processors on your computer.

Sample Image


Contemporary Intel Pentium IV processors support the so called Hyper-Threading mode. With Hyper-Threading, a real physical processor presents itself to the operating system as two independent logical processors. Hyper-Threading technology lessens process switching overhead, increasing overall system throughput. But in some calculation intensive applications, this delusion of two logical processors may lead to the loss of effectiveness if the program will start two parallel threads, which will compete for one physical processor. Thus we have to determine how many physical processors we have in our computer to adjust the number of parallel threads to the number of physical processors. The latter utility shows how we can do it from a C# program.


The CPU Counting utility is based on the following Intel documents and code samples:

Using the code

The CPU Counting utility consists of two parts - the client part and the server part. The client part written in C# calls the server function and displays its results. The server part actually determines the number of processors. It uses ASM commands extensively and therefore is written in managed C++.

The key part of the client is the call to the server function GetCPUCount.

int LogicalNum   = 0;  // Number of logical CPU per ONE PHYSICAL CPU
int PhysicalNum  = 0;  // Total number of physical processor
CPUCountStatus HTStatusFlag = 0;

HTStatusFlag = CPUCount.GetCPUCount(ref LogicalNum, ref PhysicalNum);

Don't forget to add namespace usage statement.

using CPUCounter;

Server part is implemented as a separate DLL, containing static function GetCPUCount of the class CPUCount.

// CPUCount.h

#pragma once

#using   <mscorlib.dll>
using namespace System;

namespace CPUCounter
    public __value enum CPUCountStatus

    public __value class CPUCount
            static CPUCountStatus GetCPUCount(int __gc* LogicalNum, 
                                              int __gc* PhysicalNum);

    unsigned int  HTSupported();
    unsigned int LogicalProcPerPhysicalProc();
    unsigned int GetAPIC_ID();
    CPUCountStatus DefineCPUCount(int __gc* LogicalNum, 
                                  int __gc* PhysicalNum);


Function GetCPUCount returns value of CPUCountStatus enum type. Meaning of CPUCountStatus values are:

HT_NOT_CAPABLE Hyper-threading technology doesn't exist in this system.
HT_ENABLED Hyper-threading technology is enabled in this system.
HT_DISABLED Hyper-threading technology is disabled in the hardware.
HT_SUPPORTED_NOT_ENABLED Hyper-threading technology is supported but disabled in the BIOS.
HT_CANNOT_DETECT Hyper-threading technology cannot be detected

To determine hyper-threading possibilities and the number of physical and logical processors in your computer, just copy the files CPUCounterClient.exe and CPUCounter.dll to some directory and run CPUCounterClient.exe.


This is the first version of the CPU counting utility.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gregory Gintsberg
Web Developer
Israel Israel
I am free-lance software and algorithms developer and consultant.
I program computers for almost 40 years starting with machine code to contemporary high level languages, and programming is still a greate fun for me.

You may also be interested in...


Comments and Discussions

GeneralLicense Agreement Pin
manche13-Feb-13 4:34
membermanche13-Feb-13 4:34 
Generalbad result on core i7 Pin
Alexandre GRANVAUD28-Dec-10 4:21
memberAlexandre GRANVAUD28-Dec-10 4:21 
QuestionRe: bad result on core i7 Pin
codinglearn0113-May-16 12:42
membercodinglearn0113-May-16 12:42 
QuestionHow to make this work on x64? Pin
jlilley25-Jun-09 9:13
memberjlilley25-Jun-09 9:13 
AnswerRe: How to make this work on x64? Pin
dan_linder9-Oct-09 6:50
memberdan_linder9-Oct-09 6:50 
GeneralThis article is just a copy of the Intel sample Pin
Jon3-Jan-07 0:18
memberJon3-Jan-07 0:18 
GeneralDual core Pin
khb14-Jul-06 3:48
memberkhb14-Jul-06 3:48 
GeneralRe: Dual core Pin
dan_linder9-Oct-09 7:02
memberdan_linder9-Oct-09 7:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 1 Feb 2005
Article Copyright 2005 by Gregory Gintsberg
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid