PDA

View Full Version : Determining no. of CPU cores available



cnbp173
21st April 2009, 01:17
Is there a nice easy way in Qt to determine the number of available cores on a system?

I'm looking for something that is platform independent to check the no. of CPU cores available and configure a parallel job on execution, but need to know what cores I have available to play with.

Cheers

moridinbg
21st April 2009, 07:26
Here you go. Easily googled it ;)


static int getCpuCount()
{
int cpuCount = 1;

#if defined(Q_OS_WIN32) || defined(Q_OS_WIN64)
{
SYSTEM_INFO si;
GetSystemInfo(&si);
cpuCount = si.dwNumberOfProcessors;
}
#elif defined(Q_OS_UNIX) && !defined(Q_OS_MACX)
cpuCount = sysconf(_SC_NPROCESSORS_ONLN);
#elif defined(Q_OS_MACX)
kern_return_t kr;
struct host_basic_info hostinfo;
unsigned int count;

count = HOST_BASIC_INFO_COUNT;
kr = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostinfo, &count);
if(kr == KERN_SUCCESS) {
cpuCount = hostinfo.avail_cpus;
}

#endif

if( cpuCount < 1 )
cpuCount = 1;

return cpuCount;
}

It may not be the best solution, but I have used it with no problems on several occasions.

drhex
21st April 2009, 09:56
Also see QThread::idealThreadCount()

cnbp173
21st April 2009, 17:55
Thanks guys - suppose I should have googled. I searched the forum and got nothing.

Cheers