views:

355

answers:

3

Hi!

I write a little raytracer and i'd like to query how many cpu cores (or virtual cpu cores if the cpu uses hyperthreading) the current computer offers, such that i can instanciate as many threads to get better parallel rendering.

How can I do that using C++?

thanks!

+8  A: 

You can get the number of physical processors by calling GetSystemInfo and checking the dwNumberOfProcessors field of the SYSTEM_INFO structure. You can get the number of logical processors by calling GetLogicalProcessorInformation.

Nick Meyer
BTW: GetLogicalProcessorInformation doesn't work on XP before SP3
naugtur
+2  A: 

Try the GetSystemInfo function. It returns a SYSTEM_INFO struct which has a dwNumberOfProcessors member.

luke
+2  A: 

The Win32 API function GetSystemInfo will return a SYSTEM_INFO structure with the information you need. Specifically, check the dwNumberOfProcessors member variable.

Russell Newquist