1 #include <windows.h> 2 #include <math.h> 3 int main(void) 4 { 5 SetThreadAffinityMask(GetCurrentProcess(), 0x00000001); 6 const double SPLIT=0.01; 7 const int COUNT=200; 8 const double PI=3.14159265; 9 const int INTERVAL=300; 10 DWORD busySpan[COUNT]; //array of busy time 11 DWORD idleSpan[COUNT]; //array of idle time 12 int half=INTERVAL/2; 13 double radian=0.0; 14 for(int i=0;i<COUNT;i++) 15 { 16 busySpan[i]=(DWORD)(half+(sin(PI*radian)*half)); 17 idleSpan[i]=INTERVAL-busySpan[i]; 18 radian+=SPLIT; 19 } 20 DWORD startTime=0; 21 int j=0; 22 while(true) 23 { 24 j=j%COUNT; 25 startTime=GetTickCount(); 26 while((GetTickCount()-startTime)<=busySpan[j]) 27 ; 28 Sleep(idleSpan[j]); 29 j++; 30 } 31 return 0; 32 }
参考:http://www.cnblogs.com/Ripper-Y/archive/2012/05/19/2508511.html