我使用C ++ 11线程并且需要将线程的亲和性设置为给定的处理器核心(Windows的调度程序不会智能地委托给核心,我希望控制哪个线程进入哪个核心)。
对于Windows,有
SetThreadAffinityMask(GetCurrentThread(), affinity);
单个跨平台函数在windows,linux和OSX上的作用是什么,具有底层线程库(windows thread,pthreads等)的所有排列?
答案 0 :(得分:1)
如果是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
实现必须已经处理“底层线程库的所有排列”。< / p>
自己构建一个,或使用实用程序库(我现在不能命名现有的)它可能看起来像:
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标准化。