C++ POSIX相当于boost::thread::hardware\u并发
可能重复:C++ POSIX相当于boost::thread::hardware\u并发,c++,c,linux,C++,C,Linux,可能重复: 什么是POSIX或x86、x86-64特定的系统调用,用于确定系统可以在不过度订阅的情况下运行的最大线程数?谢谢。它使用C兼容的结构,所以为什么不直接使用实际的代码呢?[libs/thread/src/*/thread.cpp] 使用pthread库: unsigned thread::hardware_concurrency() { #if defined(PTW32_VERSION) || defined(__hpux) return pthread_num_proce
什么是POSIX或x86、x86-64特定的系统调用,用于确定系统可以在不过度订阅的情况下运行的最大线程数?谢谢。它使用C兼容的结构,所以为什么不直接使用实际的代码呢?[libs/thread/src/*/thread.cpp] 使用pthread库:
unsigned thread::hardware_concurrency()
{
#if defined(PTW32_VERSION) || defined(__hpux)
return pthread_num_processors_np();
#elif defined(__APPLE__) || defined(__FreeBSD__)
int count;
size_t size=sizeof(count);
return sysctlbyname("hw.ncpu",&count,&size,NULL,0)?0:count;
#elif defined(BOOST_HAS_UNISTD_H) && defined(_SC_NPROCESSORS_ONLN)
int const count=sysconf(_SC_NPROCESSORS_ONLN);
return (count>0)?count:0;
#elif defined(_GNU_SOURCE)
return get_nprocs();
#else
return 0;
#endif
}
在windows中:
unsigned thread::hardware_concurrency()
{
SYSTEM_INFO info={{0}};
GetSystemInfo(&info);
return info.dwNumberOfProcessors;
}
它使用C兼容的结构,那么为什么不直接使用实际的代码呢?[libs/thread/src/*/thread.cpp] 使用pthread库:
unsigned thread::hardware_concurrency()
{
#if defined(PTW32_VERSION) || defined(__hpux)
return pthread_num_processors_np();
#elif defined(__APPLE__) || defined(__FreeBSD__)
int count;
size_t size=sizeof(count);
return sysctlbyname("hw.ncpu",&count,&size,NULL,0)?0:count;
#elif defined(BOOST_HAS_UNISTD_H) && defined(_SC_NPROCESSORS_ONLN)
int const count=sysconf(_SC_NPROCESSORS_ONLN);
return (count>0)?count:0;
#elif defined(_GNU_SOURCE)
return get_nprocs();
#else
return 0;
#endif
}
在windows中:
unsigned thread::hardware_concurrency()
{
SYSTEM_INFO info={{0}};
GetSystemInfo(&info);
return info.dwNumberOfProcessors;
}
本质上告诉您正在运行的处理器内核数。除非您知道自己是唯一正在运行的进程,并且您的所有线程都将保持满负载,否则您可以在不过度订阅的情况下运行的线程数不是一个定义很好的概念,并且可以动态变化。本质上告诉您正在运行的处理器内核数。除非您知道您是唯一正在运行的进程,并且您的所有线程都将保持满负载,否则您可以在不过度订阅的情况下运行的线程数不是一个很好的定义概念,并且可以动态变化。对于头文件:对于头文件: