c++11 Single-case mode
For c++11, the singleton pattern is simple! Please look at the code.
<span style= "FONT-SIZE:14PX;" > #include <iostream> #include <ratio> #include <chrono> #include <thread> #include <cmath >using namespace Std;template <typename t> class Singleton{public: static t& get_my_class_instance () c1/>{ static T instance; return instance;} ; Class a{ Private: A () {cout<< "ctor a" <<ENDL;} Friend Class singleton<a>;}; int main () {thread T1 (singleton<a>::get_my_class_instance); T1.detach (); Thread T2 (singleton<a>::get_my_ class_instance); T2.detach (); thread t3 (singleton<a>::get_my_class_instance); T3.detach (); thread t4 (Singleton <a>::get_my_class_instance); T4.detach (); Singleton<a>::get_my_class_instance ();} </span>
Next, we'll discuss how to use C++11 to make the CPU usage a sin curve
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.
The following code only uses some of the standard C + + header files, mainly using Chrono and thread, these two header files are c++11 introduced.
Talking is cheap,show you the code!
<span style= "FONT-SIZE:14PX;" > #include <iostream> #include <ratio> #include <chrono> #include <thread> #include <cmath >int Main () {using Std::chrono::system_clock; const unsigned int maxcpusleepmills=100; const double pi=3.1415926; const unsigned int samplecount=300; const double pi_2=pi*2; int Samplesinmills[samplecount]; for (unsigned int i=0;i<samplecount;i++) {samplesinmills[i]= (MAXCPUSLEEPMILLS/2) +sin (pi_2*i/samplecount) * ( MAXCPUSLEEPMILLS/2); } while (true) {for (unsigned int i=0;i<samplecount;i++) {system_clock::time_point justnow= System_clock::now ( ); Std::chrono::d uration<int,std::ratio<1,1000> > Sleepmills (samplesinmills[i]); System_clock::time_point Justlater = Justnow + sleepmills; while (System_clock::now () <justlater); Std::this_thread::sleep_for (Std::chrono::milliseconds (maxcpusleepmills-samplesinmills[i)); }} return 0;} </span>
This piece of code does not change a bit, we just use c++11 again. The future of C + + multi-threaded support will be more and more perfect (at present is very perfect), it is worth our deep learning!
The Maxcpusleepmills,samplecount in the code can be adjusted based on the CPU hardware parameters and the sample frequency of the task Manager.
The biggest problem with this code is that support for multicore processors is not good enough and often does not work as expected for multicore processors. can use multithreading technology to achieve better results, C++11 provides a hardware concurrency to get the number of a
Method Std::thread::hardware_concurrency (), utilization may be able to handle better. Why, perhaps, because of platform differences, it is possible to cause the std::thread::hardware_concurrency () return value to be inaccurate.
--------------------------------------------------------------------------------------------------------------- -----------------------------------------
reproduced in the source, thank you。 Dear friends, Learn c++14.
C++11 single-case mode with c++11 CPU utilization as sin curve