作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我使用 C++11 线程,需要设置线程与给定处理器核心的关联(Windows 的调度程序不会智能地委派给核心,我想控制哪个线程进入哪个核心)。
对于windows,有
SetThreadAffinityMask(GetCurrentThread(), affinity);
一个单一的跨平台函数看起来像什么可以在 windows、linux 和 OSX 上运行,并具有底层线程库(windows 线程、pthread 等)的所有排列?
最佳答案
如果是 C++ 的一部分,我怀疑它看起来类似于一对成员函数:
std::error_code std::thread::hardware_affinity(const std::vector<bool>&);
和免费功能:
std::error_code std::this_thread::hardware_affinity(const std::vector<bool>&);
假设我们谈论的资源与 std::thread::hardware_concurrency()
大致相同计数,那 std::thread
实现必须已经处理好“底层线程库的所有排列”。
自己构建一个,或者使用一个实用程序库(我现在不能说出一个现有的)它可能看起来像:
thread_utils::hardware_affinity(std::thread::native_handle_type, const std::vector<bool>&)
但它必须在内部支持所有排列。这并不是很难(许多应用程序出于必要已经这样做了),但它比 Stack Overflow 的答案可以容纳更多的工作。 ^_^
这种方法的一个恼人的限制是,尽管有成员函数 std::thread::native_handle()
, 没有 std::this_thread::native_handle()
方法。基本上是pending a write-up和 a good use case标准化。
关于c++ - 跨平台线程亲和函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43881005/
我是一名优秀的程序员,十分优秀!