C++11 single-case mode with c++11 CPU utilization as sin curve

Source: Internet
Author: User
Tags sin cmath

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.