根据 this StackOverflow question 的回答我该如何创建合适的 掩码的整数?
我进行了一些谷歌搜索,我发现的所有内容都使用 sched.h
中的 CPU_SET
宏,但它在 cpu_set_t
结构上运行,这些结构在以下情况下未定义使用NDK。当尝试使用 CPU_SET
链接器给我 undefined reference 错误(即使我链接到 pthread)。
最佳答案
好吧,最后我找到了一些直接从 sched.h
中获取的版本。如果有人有同样的问题并且不想花时间搜索它,我会在这里发布。这很有用。
#define CPU_SETSIZE 1024
#define __NCPUBITS (8 * sizeof (unsigned long))
typedef struct
{
unsigned long __bits[CPU_SETSIZE / __NCPUBITS];
} cpu_set_t;
#define CPU_SET(cpu, cpusetp) \
((cpusetp)->__bits[(cpu)/__NCPUBITS] |= (1UL << ((cpu) % __NCPUBITS)))
#define CPU_ZERO(cpusetp) \
memset((cpusetp), 0, sizeof(cpu_set_t))
当原始 setCurrentThreadAffinityMask
中的参数类型(来自问题中提到的帖子)被简单地替换为 cpu_set_t
时,这很有效。
关于Android设置线程亲和性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16319725/