要研究的东东啊
#include<iostream> #include<cmath> #include<windows.h> static int PERIOD = 60 * 1000; //周期ms const int COUNT = 300; //一个周期计算次数 const double GAP_LINEAR = 100; //线性函数时间间隔100ms const double PI = 3.1415926535898; //PI const double GAP = (double)PERIOD / COUNT; //周期函数时间间隔 const double FACTOR = 2 * PI / PERIOD; //周期函数的系数 static double Ratio = 0.5; //线性函数的值 0.5即50% static double Max=0.9; //方波函数的最大值 static double Min=0.1; //方波函数的最小值 typedef double Func(double); //定义一个函数类型 Func*为函数指针 typedef void Solve(Func *calc);//定义函数类型,参数为函数指针Func* inline DWORD get_time() { return GetTickCount(); //操作系统启动到现在所经过的时间ms } double calc_sin(double x) //调用周期函数solve_period的参数 { return (1 + sin(FACTOR * x)) / 2; //y=1/2(1+sin(a*x)) a=2*Pi/(60*1000) } double calc_cos(double x) { return (1 + cos(FACTOR * x)) / 2; //y=1/2(1+cos(a*x)) } double calc_absin(double x) { return fabs(sin(FACTOR * x)); } double calc_fangbo(double x) //调用周期函数solve_period的参数 { //方波函数 if(x<=PERIOD/2) return Max; else return Min; } void solve_period(Func *calc) //线程函数为周期函数 { double x = 0.0; double cache[COUNT]; for (int i = 0; i < COUNT; ++i, x += GAP) cache[i] = calc(x); int count = 0; while(1) { unsigned ta = get_time(); if (count >= COUNT) count = 0; double r = cache[count++]; DWORD busy = r * GAP; while(get_time() - ta < busy) {} Sleep(GAP - busy); } } void solve_linear(Func*) //线程函数为线性函数,参数为空 NULL { const unsigned BUSY = Ratio * GAP_LINEAR; const unsigned IDLE = (1 - Ratio) * GAP_LINEAR; while(1) { unsigned ta = get_time(); while(get_time() - ta < BUSY) {} Sleep(IDLE); } } void run(int i=4,double R=0.5,double T=60000,double max=0.9,double min=0.1) //i为输出状态,R为直线函数的值,T为周期函数的周期,max方波最大值,min方波最小值 { Ratio=R; PERIOD=T; Max=max; Min=min; //Func *func[] = {NULL ,calc_sin,calc_fangbo}; //传给Solve的参数,函数指针数组 Func *func[] = {NULL, calc_sin, calc_cos, calc_absin, calc_fangbo}; Solve *solve_func[] = { solve_linear, solve_period}; //Solve函数指针数组 //const int NUM_CPUS = 2; //双核,通用的可以用下面GetSystemInfo得到cpu数目 SYSTEM_INFO info; GetSystemInfo(&info); //得到cpu数目 const int NUM_CPUS = info.dwNumberOfProcessors; //printf("%d\n",NUM_CPUS); HANDLE handle[NUM_CPUS]; DWORD thread_id[NUM_CPUS]; //线程id switch(i) { case 4: // { if ((handle[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)solve_func[1], //CPU1正弦 (VOID*)func[1], 0, &thread_id[0])) != NULL) //创建新线程 SetThreadAffinityMask(handle[0], 1); //限定线程运行在哪个cpu上 if ((handle[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)solve_func[1], //CPU2余弦 (VOID*)func[2], 0, &thread_id[1])) != NULL) //创建新线程 SetThreadAffinityMask(handle[1], 2); //限定线程运行在哪个cpu上 if ((handle[2] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)solve_func[1], //CPU3正弦绝对值 (VOID*)func[3], 0, &thread_id[2])) != NULL) //创建新线程 SetThreadAffinityMask(handle[2], 4); //限定线程运行在哪个cpu上 if ((handle[3] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)solve_func[1], //CPU4方波 (VOID*)func[4], 0, &thread_id[3])) != NULL) //创建新线程 SetThreadAffinityMask(handle[3], 8); //限定线程运行在哪个cpu上 WaitForSingleObject(handle[0],INFINITE); //等待线程结束 break; } default: break; } } int main() { run(4,0.8,60000,0.8,0.3); }
让CPU显示正弦曲线
#include <windows.h> #include <stdlib.h> #include <math.h> const double SPLIT = 0.01; const int COUNT = 200; const double PI = 3.14159265; const int INTERVAL = 300; int main() { DWORD busySpan[COUNT]; //array of busy times DWORD idleSpan[COUNT]; //idle int half = INTERVAL/2; double radian = 0.0; for (int i = 0; i < COUNT; i++) { busySpan[i] = (DWORD)(half + sin(PI * radian) * half); idleSpan[i] = INTERVAL - busySpan[i]; radian += SPLIT; } DWORD startTime = 0; int j = 0; while(true) { j = j % COUNT; startTime = GetTickCount(); while((GetTickCount() - startTime) <= busySpan[j]) ; Sleep(idleSpan[j]); j++; } return 0; }
双核cpu使用率画正弦曲线
#include <windows.h> #include <stdlib.h> #include <iostream> #include <math.h> #include <tchar.h> const double SPLIT = 0.01; const int COUNT = 200; const double PI = 3.14159265; const int INTERVAL = 300; int main(int argc,char* argv[]) { SYSTEM_INFO systemInfo; ::GetSystemInfo(&systemInfo); std::cout << systemInfo.wProcessorArchitecture << std::endl; std::cout << systemInfo.dwProcessorType << std::endl; std::cout << "处理器数目:" << systemInfo.dwNumberOfProcessors << std::endl; std::cout << "处理器掩码:" << systemInfo.dwActiveProcessorMask << std::endl; //PDWORD_PTR process1,process2; //GetProcessAffinityMask(::GetCurrentProcess(),process1,process2); std::cout << SetProcessAffinityMask(::GetCurrentProcess(),0x00000001) << std::endl; //if(!SetThreadAffinityMask(::GetCurrentProcess(),0x00000003)) { //std::cout << "错误:" << GetLastError() << std::endl; //::MessageBox(GetActiveWindow(),_T("asdf"),_T("asdf"),MB_OK); } //绘制正弦曲线 DWORD busySpan[COUNT]; DWORD idleSpan[COUNT]; int half = INTERVAL/2; double radian = 0.0; for(int i=0;i<COUNT;i++) { busySpan[i] = (DWORD)(half + (sin(PI*radian)*half)); idleSpan[i] = INTERVAL - busySpan[i]; radian += SPLIT; } DWORD startTime = 0; int j = 0; while(true) { j=j%COUNT; startTime = GetTickCount(); while((GetTickCount()-startTime)<=busySpan[j]) ; Sleep(idleSpan[j]); j++; } return 0; }
http://blog.renren.com/share/225465617/15198723344
Keep it simple!