critcl::cproc nproc : Number of processors

JBR 9-13-2012 Cribbed from TIP 377 and other pages on the interweb.

package provide nproc 0.5

critcl::ccode {
    #ifdef _WIN32
    #include <windows.h>
    #else
    #include <unistd.h>
    #endif
}

critcl::cproc nproc {} int {
#ifdef WIN32
    SYSTEM_INFO sysinfo;
    GetSystemInfo(&sysinfo);
    return sysinfo.dwNumberOfProcessors;
#elif __MACH__
    int     count ;
    size_t  size=sizeof(count) ;

    if (sysctlbyname("hw.ncpu",&count,&size,NULL,0)) return 1;

    return count;

#elif __linux
    return sysconf(_SC_NPROCESSORS_ONLN);
#else
    return 1;
#endif
}

Example usage

$ critcl -pkg nproc.tcl
$ tclsh
% lappend auto_path lib
% package require nproc
% puts "number of processors = [nproc]"

AK - 2012-09-13 16:56:13

See also tcl-hwloc which came out of GSoC Idea: Binding To Hwloc.