标签:http os io for 2014 ar amp 管理
#include <stdio.h>
#include <windows.h>
#include <process.h>
#include <time.h>
unsigned int _stdcall thread_proc(void* arg)
{
double x = 100.0;
int r = 0;
srand((unsigned)time(0) );
while(1)
{
r = rand();//模拟运算
x/=r;
}
return 0;
}
int main()
{
SYSTEM_INFO s_info={0};
DWORD cpu_count = 0;
size_t i=0;
HANDLE* hThread;
GetSystemInfo(&s_info);
//取系统CPU个数
cpu_count = s_info.dwNumberOfProcessors;
//根据CPU个数创建相应数量的线程
hThread = calloc(cpu_count,sizeof(HANDLE) );
for(i=0;i<cpu_count;i++)
{
//创建初始状态为挂起
hThread[i] = (HANDLE)_beginthreadex(NULL,0,thread_proc,NULL,CREATE_SUSPENDED,NULL);
/*
*00000001:CPU0
*00000010:CPU1
*00000100:CPU2
*00001000:CPU3
**/
SetThreadAffinityMask(hThread[i], 1<<i);//CPU掩码
ResumeThread(hThread[i]);//唤醒
}
printf("打开任务管理器查看...\n");
for(i=0; i<cpu_count;i++)
{
WaitForSingleObject(hThread[i],INFINITE);
}
free(hThread);
return 0;
}

设置线程的亲缘性(指定其所运行的CPU核心),布布扣,bubuko.com
标签:http os io for 2014 ar amp 管理
原文地址:http://my.oschina.net/mlgb/blog/298553