【问题标题】:setting CPU affinity error on windows pthread_setaffinity_np在 Windows pthread_setaffinity_np 上设置 CPU 亲和性错误
【发布时间】:2021-03-09 05:59:18
【问题描述】:

我有一个多线程项目。我想将 CPU 亲和性设置为特定线程。我在 Windows 上工作并使用 MinGW 编译器。但是当我初始化类型 cpu_set_t 时,我有一个错误“类型 cpu_set_t 未在此范围内声明”。我不明白原因。我期待你的帮助!谢谢!

#define _GNU_SOURCE
#include <iostream>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
int main() 
{
   int core_id = 1;
   cpu_set_t cpuset; // error here
   CPU_ZERO(&cpuset);
   CPU_SET(core_id, &cpuset);
   pthread_t current_thread = pthread_self();    
   pthread_setaffinity_np(current_thread, sizeof(cpu_set_t), &cpuset);
}

【问题讨论】:

  • 我认为你使用的 pthread 库没有实现pthread_setaffinity_np()

标签: windows pthreads mingw eclipse-cdt pthreads-win32


【解决方案1】:

Windows 本身不支持 pthreads 标准。 MinGW 项目使用 winpthreads 库作为原生 Windows 线程的包装器。

'_np' 后缀 - 表示不可移植。 pthreads(posix) 标准确实需要这些 *_np 函数。

当前版本的winpthreads没有很多其他操作系统如Linux上可以看到的*_np函数。

您可以编写自己的函数来更改 Windows 线程关联性并从您的线程中调用它。 类似的东西:

#include <windows.h>

// Bind thread to CPUs
// Return previous mask value on success, 0 on failure
DWORD_PTR BindThreadToCPU(
                            DWORD mask // 1  -  bind to cpu 0
                                       // 4  -  bind to cpu 2
                                       // 15 -  bind to cpu 0,1,2,3
                          )
{
    HANDLE th = GetCurrentThread();
    DWORD_PTR prev_mask = SetThreadAffinityMask(th, mask);
    return prev_mask;
}

注意:线程关联掩码必须是进程关联掩码的子集。

还有来自Microsoft documentation 的关于SetThreadAffinityMask() 及其调度程序的有趣引用:

线程亲和性强制线程在特定的处理器子集上运行。 通常应避免设置线程亲和性,因为它会干扰 调度程序能够跨处理器有效地调度线程。 这会降低并行处理产生的性能增益。 线程亲和性的适当使用是测试每个处理器。

在现代版本的 Windows 中,SetThreadIdealProcessor() 函数对调度程序更友好,并允许您为线程设置首选 CPU(但没有任何严格的亲和力保证)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 2018-05-19
    相关资源
    最近更新 更多