我编写了一个测试程序来绑定 CPU 上的线程。这是我的测试代码:
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
DWORD WINAPI ThreadFunc(LPVOID pM) {
while(1) {
printf("Doesn't work anymore"); //If i add this printf function,the setthreadaffinitymask seems doesn't work anymore.Which u can see from two pictures i post.
}
}
int main()
{
HANDLE h;
DWORD_PTR RetFlag;
h = CreateThread(NULL, 0, ThreadFunc, NULL, 0, NULL);
if((RetFlag = SetThreadAffinityMask(h, 0x08)) == 0)
printf("SetThreadAffinityMask fails\n");
WaitForSingleObject(h, INFINITE);
return 0;
}
当然,正确的结果应该像图片一个。但是如果我在线程函数中添加 printf() 函数会发生什么?有什么我不知道的技巧吗?谢谢...