CPU多核的线程控制

cpu下SetThreadAffinityMask运用框架
2009年10月22日 星期四 23:04

static DWORD WINAPI ZFive5Proc(LPVOID p)
{
   //自己需要控制代码开始
   _asm{
      push eax
      mov eax,1
      pop eax
    }
  
    sprintf((char*)p,"zfive5! good");
   //自己需要控制代码结束

   return 0;
}

void CCPUDlg::OnOK()
{
// TODO: Add extra validation here

//CDialog::OnOK();

char szbuf[200];
DWORD id;
HANDLE hHandle=CreateThread(NULL,0,ZFive5Proc,&szbuf,CREATE_SUSPENDED ,&id);
if(hHandle==NULL)
{
AfxMessageBox("zfive5! error");
return;
}
//CPU1
SetThreadAffinityMask(hHandle,1);
//CPU2
//SetThreadAffinityMask(hHandle,2);
ResumeThread(hHandle);
if(WaitForSingleObject(hHandle,INFINITE)!=WAIT_OBJECT_0)
{
AfxMessageBox("zfive5! error");
     CloseHandle(hHandle);
return;
}

CloseHandle(hHandle);
//TRACE("%s/r/n",szbuf);
AfxMessageBox(szbuf);
return;

}


这样就可以让线程的代码ZFive5Proc在cpu1上执行了!

 

 

/

 

 

MSDN上的描述:

SetThreadAffinityMask
The SetThreadAffinityMask function sets a processor affinity mask for the specified thread.

DWORD_PTR SetThreadAffinityMask(
HANDLE hThread,
DWORD_PTR dwThreadAffinityMask
);

Parameters
hThread
[in] Handle to the thread whose affinity mask is to be set.
This handle must have the THREAD_SET_INFORMATION and THREAD_QUERY_INFORMATION access rights. For more information, see Thread Security and Access Rights.

dwThreadAffinityMask
[in] Affinity mask for the thread.
Windows Me/98/95:  This value must be 1.

Return Values
If the function succeeds, the return value is the thread's previous affinity mask.

Windows Me/98/95:  The return value is 1. To succeed, hThread must be valid and dwThreadAffinityMask must be 1.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks
A thread affinity mask is a bit vector in which each bit represents the processors that a thread is allowed to run on.

A thread affinity mask must be a proper subset of the process affinity mask for the containing process of a thread. A thread is only allowed to run on the processors its process is allowed to run on.



通过调用SetThreadAffinityMask,就能为各个线程设置亲缘性屏蔽:

DWORD_PTR SetThreadAffinityMask(HANDLE hThread, DWORD_PTR dwThreadAffinityMask);


该函数中的h T h r e a d参数用于指明要限制哪个线程, dwThreadAffinityMask用于指明该线程能够在哪个CPU上运行。dwThreadAffinityMask必须是进程的亲缘性屏蔽的相应子集。返回值是线程的前一个亲缘性屏蔽。

因此,若要将3个线程限制到CPU1、2和3上去运行,可以这样操作:

//Thread 0 can only run on CPU 0.

SetThreadAffinityMask(hThread0, 0x00000001); //第0位是1

//Threads 1, 2, 3 run on CPUs 1, 2, 3.//第1 2 3位是1

SetThreadAffinityMask(hThread1, 0x0000000E);

SetThreadAffinityMask(hThread2, 0x0000000E);

SetThreadAffinityMask(hThread3, 0x0000000E);




本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/W511522329/archive/2010/03/06/5352597.aspx

#include "stdafx.h"
#include <windows.h>
#include <string>
#include <iostream>
void running(int seconds) {
Sleep(seconds*1000);
std::cout<<"sleep for "<<seconds<<"(s)"<<std::endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
SetThreadAffinityMask(GetCurrentThread(), 1);
LARGE_INTEGER start, end;
LARGE_INTEGER freq;
//timeConsuming();
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&start);//start


std::cout<<"start.QuadPart = "<<start.QuadPart<<std::endl; //output start
running(10); //running 10 seconds
QueryPerformanceCounter(&end); //end
std::cout<<"end.QuadPart = "<<end.QuadPart<<std::endl; //output end

std::cout<<"consume value = end.QuadPart - start.QuadPart = "<<(end.QuadPart - start.QuadPart)<<std::endl;
std::cout<<"(consume value/(double)freq.QuadPart) Time consumed = "<<(end.QuadPart - start.QuadPart)/(double)freq.QuadPart<<"(s)"<<std::endl; //output consumed time
return 0;
}

start.QuadPart = 49102789906513
sleep for10(s)
end.QuadPart = 49127801303663
consume value = end.QuadPart - start.QuadPart = 25011397150
(consume value/(double)freq.QuadPart) Time consumed = 10.0046(s)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值