[英]SetThreadAffinityMask has no effect
I want a test program only run on cpu1 ,so i write the code like that 我想要一个仅在cpu1上运行的测试程序,所以我编写了这样的代码
#include <iostream>
#include <windows.h>
int main(){
::SetThreadAffinityMask(::GetCurrentProcess(),1);
while(1)
;
return 0;
}
but when I open the task manager, I find CPU1 is not full used, and the percent of it's usage is always changing.Why?CPU1 should be 100 percent usage? 但是当我打开任务管理器时,我发现CPU1没有被完全使用,并且其使用百分比始终在变化。为什么CPU1应该是100%使用率? sorry to my english.
对不起我的英语。
SetThreadAffinityMask
sets the affinity of a THREAD not of the whole process. SetThreadAffinityMask
设置THREAD的亲和力而不是整个过程的亲和力。 If you check the return value you should see that SetThreadAffinityMask
is failing. 如果检查返回值,则应该看到
SetThreadAffinityMask
失败。 To get the current thread use GetCurrentThread()
not GetCurrentProcess()
. 要获取当前线程,请使用
GetCurrentThread()
而不是GetCurrentProcess()
。
ie change your code to 即将您的代码更改为
SetThreadAffinityMask(GetCurrentThread(),1)
If you want to set the affinity of the whole process use 如果要设置整个过程的亲和力,请使用
SetProcessAffinityMask(GetCurrentProcess(),1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.