Results 1 to 4 of 4

Thread: Determining no. of CPU cores available

  1. #1
    Join Date
    May 2008
    Posts
    25
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Determining no. of CPU cores available

    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

  2. #2
    Join Date
    Jun 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Determining no. of CPU cores available

    Here you go. Easily googled it
    Qt Code:
    1. static int getCpuCount()
    2. {
    3. int cpuCount = 1;
    4.  
    5. #if defined(Q_OS_WIN32) || defined(Q_OS_WIN64)
    6. {
    7. SYSTEM_INFO si;
    8. GetSystemInfo(&si);
    9. cpuCount = si.dwNumberOfProcessors;
    10. }
    11. #elif defined(Q_OS_UNIX) && !defined(Q_OS_MACX)
    12. cpuCount = sysconf(_SC_NPROCESSORS_ONLN);
    13. #elif defined(Q_OS_MACX)
    14. kern_return_t kr;
    15. struct host_basic_info hostinfo;
    16. unsigned int count;
    17.  
    18. count = HOST_BASIC_INFO_COUNT;
    19. kr = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostinfo, &count);
    20. if(kr == KERN_SUCCESS) {
    21. cpuCount = hostinfo.avail_cpus;
    22. }
    23.  
    24. #endif
    25.  
    26. if( cpuCount < 1 )
    27. cpuCount = 1;
    28.  
    29. return cpuCount;
    30. }
    To copy to clipboard, switch view to plain text mode 
    It may not be the best solution, but I have used it with no problems on several occasions.

  3. #3
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Determining no. of CPU cores available


  4. #4
    Join Date
    May 2008
    Posts
    25
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Determining no. of CPU cores available

    Thanks guys - suppose I should have googled. I searched the forum and got nothing.

    Cheers

Similar Threads

  1. Problem determining size of QGraphicsView
    By Bocki in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2008, 14:54
  2. Determining when mouse over widget without events
    By Kimmo in forum Qt Programming
    Replies: 2
    Last Post: 7th November 2007, 10:48
  3. Determining Logged In State
    By TheGrimace in forum Qt Programming
    Replies: 2
    Last Post: 22nd October 2007, 15:40
  4. Any ideas on determining cpu load in run-time?
    By a550ee in forum Qt Programming
    Replies: 3
    Last Post: 24th November 2006, 08:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.