小编use*_*476的帖子

如何将外部DLL限制为一个CPU?

我有一个程序,我想只在一个CPU上运行,所以它不会占用太多的系统资源.问题是,它调用外部DLL,自动使用所有可用的CPU内核.我没有外部DLL的源代码.如何将DLL限制为仅使用一个CPU?

编辑:感谢您的帮助,这是我用来限制到一个CPU(Windows)的代码:

// Limit the process to only 1 thread so we don't chew up system resources
HANDLE ProcessHandle = GetCurrentProcess();
DWORD ProcessAffinityMask;
DWORD SystemAffinityMask;
if(GetProcessAffinityMask(ProcessHandle,&ProcessAffinityMask,&SystemAffinityMask)
    && SystemAffinityMask != 0)
{
    // Limit to 1 thread by masking all but 1 bit of the system affinity mask
    DWORD NewProcessAffinityMask = ((SystemAffinityMask-1) ^ SystemAffinityMask) & SystemAffinityMask;
    SetProcessAffinityMask(ProcessHandle,NewProcessAffinityMask);
}
Run Code Online (Sandbox Code Playgroud)

编辑:结果Brannon设置流程优先级的方法更适合我想要的,这是为了防止流程咀嚼资源.这是代码(Windows):

// Make the process low priority so we don't chew up system resources
HANDLE ProcessHandle = GetCurrentProcess();
SetPriorityClass(ProcessHandle,BELOW_NORMAL_PRIORITY_CLASS);
Run Code Online (Sandbox Code Playgroud)

c++ dll multithreading external-dependencies

2
推荐指数
2
解决办法
1414
查看次数

标签 统计

c++ ×1

dll ×1

external-dependencies ×1

multithreading ×1