句柄 std::thread::hardware_concurrency()
Handle std::thread::hardware_concurrency()
在我关于 std::thread
的 中,有人建议我使用 std::thread::hardware_concurrency()
。我在某处读到(我找不到它,看起来像是本地代码存储库或其他东西),该功能未针对 4.8 之前的 g++ 版本实现。
事实上,我和 this user. The function will simply return 0. I found in this 回答用户实施时处于相同的受害者位置。这个回答好不好欢迎大家评论!
所以我想在我的代码中这样做:
unsinged int cores_n;
#if g++ version < 4.8
cores_n = my_hardware_concurrency();
#else
cores_n = std::thread::hardware_concurrency();
#endif
但是,我可以找到实现此结果的方法。我该怎么办?
基于常见的预定义宏 link,由 Joachim 友情提供,我做了:
int p;
#if __GNUC__ >= 5 || __GNUC_MINOR__ >= 8 // 4.8 for example
const int P = std::thread::hardware_concurrency();
p = (trees_no < P) ? trees_no : P;
std::cout << P << " concurrent threads are supported.\n";
#else
const int P = my_hardware_concurrency();
p = (trees_no < P) ? trees_no : P;
std::cout << P << " concurrent threads are supported.\n";
#endif
除了使用 GCC Common Predefined Macros 还有另一种方法:检查 std::thread::hardware_concurrency()
returns 是否为零,这意味着该功能尚未(尚未)实现.
unsigned int hardware_concurrency()
{
unsigned int cores = std::thread::hardware_concurrency();
return cores ? cores : my_hardware_concurrency();
}
您可能会受到 awgn's source code(GPL v2 许可)的启发,实施 my_hardware_concurrency()
auto my_hardware_concurrency()
{
std::ifstream cpuinfo("/proc/cpuinfo");
return std::count(std::istream_iterator<std::string>(cpuinfo),
std::istream_iterator<std::string>(),
std::string("processor"));
}
在我关于 std::thread
的 std::thread::hardware_concurrency()
。我在某处读到(我找不到它,看起来像是本地代码存储库或其他东西),该功能未针对 4.8 之前的 g++ 版本实现。
事实上,我和 this user. The function will simply return 0. I found in this 回答用户实施时处于相同的受害者位置。这个回答好不好欢迎大家评论!
所以我想在我的代码中这样做:
unsinged int cores_n;
#if g++ version < 4.8
cores_n = my_hardware_concurrency();
#else
cores_n = std::thread::hardware_concurrency();
#endif
但是,我可以找到实现此结果的方法。我该怎么办?
基于常见的预定义宏 link,由 Joachim 友情提供,我做了:
int p;
#if __GNUC__ >= 5 || __GNUC_MINOR__ >= 8 // 4.8 for example
const int P = std::thread::hardware_concurrency();
p = (trees_no < P) ? trees_no : P;
std::cout << P << " concurrent threads are supported.\n";
#else
const int P = my_hardware_concurrency();
p = (trees_no < P) ? trees_no : P;
std::cout << P << " concurrent threads are supported.\n";
#endif
除了使用 GCC Common Predefined Macros 还有另一种方法:检查 std::thread::hardware_concurrency()
returns 是否为零,这意味着该功能尚未(尚未)实现.
unsigned int hardware_concurrency()
{
unsigned int cores = std::thread::hardware_concurrency();
return cores ? cores : my_hardware_concurrency();
}
您可能会受到 awgn's source code(GPL v2 许可)的启发,实施 my_hardware_concurrency()
auto my_hardware_concurrency()
{
std::ifstream cpuinfo("/proc/cpuinfo");
return std::count(std::istream_iterator<std::string>(cpuinfo),
std::istream_iterator<std::string>(),
std::string("processor"));
}