其实这也没什么值得惊讶的,因为在理解了操作系统调度,以及cpu的指令周期后,都可以用程序模拟出来
下面是编程之美给出的正弦曲线,在一定间隔内,一半时间运行,一半时间sleep。Windows自带的perfmon可以查看详细曲线
#include<iostream>
#include<string.h>
#include<sstream>
#include<set>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<Windows.h>
#include<math.h>
const double SPLIT = 0.001;
const int COUNT = 2000;
const double PI = acos(-1);
const int INTERVAL = 30;
int main(){
double busyspan[COUNT];
double idlespan[COUNT];
double radian = 0.0;
int half = INTERVAL / 2;
for (int i = 0; i < COUNT; i++) {
busyspan[i] = (double)(half + (sin(PI*radian)));
idlespan[i] = INTERVAL - busyspan[i];
radian += SPLIT;
}
double starttime = 0;
int j = 0;
while (true) {
j = j % COUNT;
starttime = GetTickCount();
while ((GetTickCount() - starttime) < busyspan[j]);
Sleep(idlespan[j]);
j++;
}
return 0;
}