标签:style blog io color os 使用 for sp div
这题目是老生常谈了,不过多数代码都是针对windows。如今C++11/14让我们可以实现跨平台,使用这份代码几乎可以运行在任何平台。
talking is cheap,show you the code!
1 #include <iostream> 2 #include <ratio> 3 #include <chrono> 4 #include <thread> 5 #include <cmath> 6 int main() 7 { 8 using std::chrono::system_clock; 9 const unsigned int maxCpuSleepMills=100; 10 const double PI=3.1415926; 11 const unsigned int sampleCount=300; 12 const double PI_2=PI*2; 13 int sampleSinMills[sampleCount]; 14 for(unsigned int i=0;i<sampleCount;i++) 15 { 16 sampleSinMills[i]=(maxCpuSleepMills/2)+sin(PI_2*i/sampleCount)*(maxCpuSleepMills/2); 17 } 18 while(true){ 19 for(unsigned int i=0;i<sampleCount;i++){ 20 system_clock::time_point justNow= system_clock::now(); 21 std::chrono::duration<int ,std::ratio<1,1000> > sleepMills(sampleSinMills[i]); 22 system_clock::time_point justLater = justNow + sleepMills; 23 while(system_clock::now()<justLater); 24 std::this_thread::sleep_for(std::chrono::milliseconds(maxCpuSleepMills-sampleSinMills[i])); 25 } 26 27 } 28 return 0; 29 }
代码思路一点没有变的。
标签:style blog io color os 使用 for sp div
原文地址:http://www.cnblogs.com/tangzhenqiang/p/4060085.html