LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

the number of processors

I am a novice in multithreading programing. I have a dual-CPU PC and I try to get the number of the CPU via the function:"CmtGetNumProcessor". Theoretically, I only have two numbers of the CPU, but I got four numbers after several check. I am confused about this phenomenon. The following is my code:

int Num,ID,CID;

int main (int argc, char *argv[])
{

//get the number of CPU which process the program
Num = CmtGetNumProcessors ();
printf("The number of CPU: %d \n",&Num);

//Returns the ID of the main thread of the program
ID = CmtGetMainThreadID ();
printf("The ID of the main thread of the program: %d \n",&ID);

//Returns the ID of the current thread
CID = CmtGetCurrentThreadID ();
printf("The ID of the current thread of the program: %d \n",&CID);

getchar();
return;
}



Thank you
0 Kudos
Message 1 of 6
(3,060 Views)
if you open up task manager, how many CPU does it identify? You can go to the Processes tab and count the number of CPU Usage History graphs that show up. If you have hyper threading enabled, each CPU shows up an 2 CPUs to Windows. This would explain what you are seeing.
Bilal Durrani
NI
Message 2 of 6
(3,047 Views)
Hi Bilal,
Thank you for your information. As you mentioned, I actually use dual Xeon CPUs with Hyperthreading technology after I checked my hardware specification. I still have questions: is the number of processors assigned randomly by OS or by CPU?? Or is it following some kind of the rule?? Due to trying to set the preferred processor for a thread, I need to know the number of processors. You also could a better way to achieve it!! Thank you!!
0 Kudos
Message 3 of 6
(3,036 Views)
Hi 48105.

You need to modify your code. This line...
printf("The number of CPU: %d \n",&Num);
...prints the _address_ of the variable "Num", not its value.

Change the line to...
printf("The number of CPU: %d \n",Num);

The same applies to the other 2 calls to printf().

Regards,
Colin.
Message 4 of 6
(3,033 Views)
Hi Colin,

Thanks for your help!


Regards,
Mitch.
0 Kudos
Message 5 of 6
(3,023 Views)
You might be able to find more information if you google the newsgroups. I do not know whether Windows assigns processor numbers in a certain order. But I would think this should not matter anyway since the processor order will not change once the system is booted up.

In my opinion, it is probably best to rely on the Windows Scheduler to determine which thread should run on which CPU. I would think that Windows would automatically use an idle CPU to service a thread if the other CPU's were busy. Have you noticed a considerable difference by setting a preferred thread to a CPU?
Bilal Durrani
NI
0 Kudos
Message 6 of 6
(3,016 Views)