The topic is a cliché, but most of the code is for Windows. Today C++11/14 allows us to implement cross-platform and use this code to run almost any platform.
Talking is cheap,show you the code!
1#include <iostream>2#include <ratio>3#include <chrono>4#include <thread>5#include <cmath>6 intMain ()7 {8 usingStd::chrono::system_clock;9 ConstUnsignedintmaxcpusleepmills= -;Ten Const DoublePi=3.1415926; One ConstUnsignedintSamplecount= -; A Const Doublepi_2=pi*2; - intSamplesinmills[samplecount]; - for(unsignedintI=0; i<samplecount;i++) the { -Samplesinmills[i]= (maxcpusleepmills/2) +sin (pi_2*i/samplecount) * (maxcpusleepmills/2); - } - while(true){ + for(unsignedintI=0; i<samplecount;i++){ -System_clock::time_point justnow=System_clock::now (); +Std::chrono::d uration<int,std::ratio<1, +> >sleepmills (Samplesinmills[i]); ASystem_clock::time_point Justlater = Justnow +Sleepmills; at while(System_clock::now () <justlater); -Std::this_thread::sleep_for (Std::chrono::milliseconds (maxcpusleepmills-samplesinmills[i])); - } - - } - return 0; in}
The code idea has not changed a bit.
How to use c++11 to make the CPU usage a sin curve