在这些平台上面已经测试过,OJ8K,没有问题。
但是它依赖于这篇文章指示的编译器平台宏判断定义。
C/C++ MACOS、Windows、Linux、HarmonyOS 平台宏判断-CSDN博客
获取CPU核心数量
int GetProcesserCount() noexcept {
int count = 0;
#if defined(_WIN32) || defined(_WIN64)
SYSTEM_INFO si;
::GetSystemInfo(&si);
count = si.dwNumberOfProcessors;
#elif defined(__ANDROID_API__) && __ANDROID_API__ >= 23
count = get_nprocs();
#elif defined(_SC_NPROCESSORS_ONLN)
count = sysconf(_SC_NPROCESSORS_ONLN); // MAOOS && LINUX
#else
count = std::thread::hardware_concurrency();
#endif
if (count < 1) {
count = 1;
}
return count;
}
获取当前进程ID
int GetCurrentProcessId() noexcept {
#if defined(_WIN32) || defined(_WIN64)
return ::GetCurrentProcessId();
#else
return ::getpid();
#endif
}
获取当前线程ID
int64_t GetCurrentThreadId() noexcept {
#if defined(_WIN32) || defined(_WIN64)
return ::GetCurrentThreadId();
#else
// https://android.googlesource.com/platform/bionic/+/master/libc/bionic/gettid.cpp
// ::gettid();
#if defined(SYS_gettid)
return syscall(SYS_gettid); /* syscall(__NR_gettid) or syscall(SYS_gettid); */
#elif defined(__NR_gettid) || defined(_ANDROID)
return syscall(__NR_gettid);
#else
/* https://elliotth.blogspot.com/2012/04/gettid-on-mac-os.html */
uint64_t tid;
pthread_threadid_np(NULL, &tid);
return static_cast<int64_t>(tid);
#endif
#endif
}