gpt4 book ai didi

c++ - 跨平台线程亲和函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:09:39 26 4
gpt4 key购买 nike

我使用 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-upa good use case标准化。

关于c++ - 跨平台线程亲和函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43881005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com