`get thread count` C++ Examples

24 C++ code examples are found related to "get thread count". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: threads.cpp    From foo_mediacontrol with BSD 2-Clause "Simplified" License 6 votes vote down vote up
t_size getOptimalWorkerThreadCount() {
#ifdef PFC_WINDOWS_DESKTOP_APP
		DWORD_PTR mask,system;
		t_size ret = 0;
		GetProcessAffinityMask(GetCurrentProcess(),&mask,&system);
		for(t_size n=0;n<sizeof(mask)*8;n++) {
			if (mask & ((DWORD_PTR)1<<n)) ret++;
		}
		if (ret == 0) return 1;
		return ret;
#else
		return std::thread::hardware_concurrency();
#endif


#if 0 // OSX
        size_t len;
        unsigned int ncpu;
        
        len = sizeof(ncpu);
        sysctlbyname ("hw.ncpu",&ncpu,&len,NULL,0);
        
        return ncpu;
#endif
	} 
Example 2
Source File: os_linux.cpp    From async-profiler with Apache License 2.0 6 votes vote down vote up
int getThreadCount() {
        char buf[512];
        int fd = open("/proc/self/stat", O_RDONLY);
        if (fd == -1) {
            return 0;
        }

        int thread_count = 0;
        if (read(fd, buf, sizeof(buf)) > 0) {
            char* s = strchr(buf, ')');
            if (s != NULL) {
                // Read 18th integer field after the command name
                for (int field = 0; *s != ' ' || ++field < 18; s++) ;
                thread_count = atoi(s + 1);
            }
        }

        close(fd);
        return thread_count;
    } 
Example 3
Source File: lock-tester.cpp    From HSCC with GNU General Public License v2.0 6 votes vote down vote up
static void GetThreadCount(ADDRINT threadCount)
{
    // Capture the number of worker threads in the application.
    // This is the number of threads that will execute DoLockTest().
    //
    RunningWorkers = threadCount;

    if (TestType == TEST_SEMAPHORE)
    {
        if (threadCount != 2)
        {
            std::cout << "Test 'semaphore' requires exactly two worker threads" << std::endl;
            PIN_ExitProcess(1);
        }
    }

    if (TestType == TEST_TRYLOCKS)
    {
        if (threadCount != 1)
        {
            std::cout << "Test 'trylocks' requires exactly one worker thread" << std::endl;
            PIN_ExitProcess(1);
        }
    }
} 
Example 4
Source File: gtest-port.cc    From simcc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
size_t GetThreadCount() {
  const task_t task = mach_task_self();
  mach_msg_type_number_t thread_count;
  thread_act_array_t thread_list;
  const kern_return_t status = task_threads(task, &thread_list, &thread_count);
  if (status == KERN_SUCCESS) {
    // task_threads allocates resources in thread_list and we need to free them
    // to avoid leaks.
    vm_deallocate(task,
                  reinterpret_cast<vm_address_t>(thread_list),
                  sizeof(thread_t) * thread_count);
    return static_cast<size_t>(thread_count);
  } else {
    return 0 
Example 5
Source File: system_compatibility.cpp    From persistent-rnn with Apache License 2.0 5 votes vote down vote up
unsigned int getHardwareThreadCount()
{
#ifdef _WIN32
    SYSTEM_INFO sysinfo;
    GetSystemInfo(&sysinfo);

    return sysinfo.dwNumberOfProcessors;
#elif __APPLE__
    int nm[2];
    size_t len = 4;
    uint32_t count;

    nm[0] = CTL_HW;
    nm[1] = HW_AVAILCPU;
    sysctl(nm, 2, &count, &len, NULL, 0);

    if(count < 1)
    {
        nm[1] = HW_NCPU;
        sysctl(nm, 2, &count, &len, NULL, 0);
        if(count < 1)
        {
            count = 1;
        }
    }
    return std::max(1U, count);
#elif __GNUC__
    return sysconf(_SC_NPROCESSORS_ONLN);
#endif
} 
Example 6
Source File: mt-worker-unix.cpp    From manul with Apache License 2.0 5 votes vote down vote up
static unsigned GetThreadCount(int argc, char **argv)
{
    // If there's no explicit thread parameter, use the number of CPU's
    // but not less than 2 and not more than 8.
    //
    if (argc <= 1)
    {
        unsigned ncpus = GetCpuCount();
        if (ncpus < 2)
            ncpus = 2;
        if (ncpus > 8)
            ncpus = 8;
        return ncpus;
    }

    if (std::strcmp(argv[1], "-t") != 0)
    {
        std::cerr << "Unknown option: " << argv[1] << std::endl;
        return 0;
    }

    if (argc < 3)
    {
        std::cerr << "Must specify a parameter to '-t'" << std::endl;
        return 0;
    }

    char *end;
    unsigned long val = std::strtoul(argv[2], &end, 10);
    if (*(argv[2]) == '\0' || val > UINT_MAX || val == 0 || *end != '\0')
    {
        std::cerr << "Invalid parameter to -t: " << argv[2] << std::endl;
        return 0;
    }
    return static_cast<unsigned>(val);
} 
Example 7
Source File: threads.cpp    From odgi with MIT License 5 votes vote down vote up
int get_thread_count(void) {
    int thread_count = 1;
#pragma omp parallel
    {
#pragma omp master
        thread_count = omp_get_num_threads();
    }
    return thread_count;
}