C++11单例模式,另附C++11令CPU占用率为sin曲线

C++11 单例模式

对于C++11来说,单例模式就是这样简单!请看代码。

<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()
    {
        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>

接下来,再讨论如何使用C++11令CPU占用率为sin曲线

这题目是老生常谈了,不过多数代码都是针对windows。如今C++11/14让我们可以实现跨平台,使用这份代码几乎可以运行在任何平台。

下面这段代码只使用了标准C++的一些头文件,其中主要使用chrono和thread,这两个头文件都是C++11引入的。

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::duration<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>

这段代码思路一点没有变的,我们只是用C++11重新了而已。未来C++对多线程的支持会越来越完善(目前已经很完善了),值得我们深入的学习!

代码中的maxCpuSleepMills,sampleCount都是可以根据CPU硬件参数的和任务管理器的采样频率调整的。

这段代码最大的问题是对多核处理器支持不够好,对于多核处理器往往达不到预期效果。可以利用多线程技术达到更好的效果,C++11提供了一个获取硬件并发数的一个

方法std::thread::hardware_concurrency(),利用也许能够处理更好。为什么说也许,因为平台差异,有可能导致std::thread::hardware_concurrency()返回值不准确。

--------------------------------------------------------------------------------------------------------------------------------------------------------

转载标明出处,谢谢。亲爱的朋友们,抓紧学习C++14吧。

时间: 2024-08-25 13:00:13

C++11单例模式,另附C++11令CPU占用率为sin曲线的相关文章

如何使用C++11令CPU占用率为sin曲线

这题目是老生常谈了,不过多数代码都是针对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 us

在rhel6上安装11.2.0.3 or 11.2.0.4 db sofrware时报缺少&quot;elfutils-libelf-devel-0.97&quot; And &quot;pdksh-5.2.14&quot;

Installing 11.2.0.3 Or 11.2.0.4 (32-bit (x86) or 64-bit (x86-64) ) On RHEL6 Reports That Packages "elfutils-libelf-devel-0.97" And "pdksh-5.2.14" Are Missing (PRVF-7532) (Doc ID 1454982.1) Applies to: Oracle Universal Installer - Versi

PSU 从11.2.0.3.0 -&gt; 11.2.0.3.11 遇冲突解决全程

Oracle rdbms 打psu从11.2.0.3.0升级到11.2.0.3.11 参考patch :18522512 停应用,停监听与DB,将db的oracle_home下的OPatch目录加到环境变化PATH路径下,因为接下来要用opatch工具. 报错,说这个opatch工具太旧,要下载新的. The OPatch version being used (11.2.0.1.7) doesn't meet the minimum version required by the patch(

11.1 LAMP架构介绍 11.2 MySQL、MariaDB介绍 11.3/11.4/11.5 MySQL安装

11.1 LAMP架构介绍 11.2 MySQL.MariaDB介绍 11.3/11.4/11.5 MySQL安装 扩展 mysql5.5源码编译安装 http://www.aminglinux.com/bbs/thread-1059-1-1.html mysql5.7二进制包安装(变化较大) http://www.apelearn.com/bbs/thread-10105-1-1.html 11.1 LAMP架构介绍 Linux + Apache(httpd)+ MySQL + PHP  PH

C++11 并发指南一(C++11 多线程初探)(转)

引言 C++11 自2011年发布以来已经快两年了,之前一直没怎么关注,直到最近几个月才看了一些 C++11 的新特性,今后几篇博客我都会写一些关于 C++11 的特性,算是记录一下自己学到的东西吧,和大家共勉. 相信 Linux 程序员都用过 Pthread, 但有了 C++11 的 std::thread 以后,你可以在语言层面编写多线程程序了,直接的好处就是多线程程序的可移植性得到了很大的提高,所以作为一名 C++ 程序员,熟悉 C++11 的多线程编程方式还是很有益处的. 如果你对 C+

int和integer;Math.round(11.5)和Math.round(-11.5)

int是java提供的8种原始数据类型之一.Java为每个原始类型提供了封装类,Integer是java为int提供的封装类.int的默认值为0,而Integer的默认值为null,即Integer可以区分出未赋值和值为0的区别,int则无法表达出未赋值的情况,例如,要想表达出没有参加考试和考试成绩为0的区别,则只能使用Integer.在JSP开发中,Integer的默认为null,所以用el表达式在文本框中显示时,值为空白字符串,而int默认的默认值为0,所以用el表达式在文本框中显示时,结果

设计模式第六讲:单例模式(附优化方案)

单例模式 一:模式定义 一个类有且仅有一个实例,并且自行实例化向整个系统提供. 二:模式特点 通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源. 三:使用场景 希望在系统中某个类的对象只能存在一个. 四:具体实现 单例模式有两种写法: 1.懒汉式 2.饿汉式 懒汉式实现: public class Single { private static Single instance; private Singleton (){} public

九周第四次课(2月26日) 11.1 LAMP架构介绍 11.2 MySQL、MariaDB介绍 11.3/11.4/11.5 MySQL安装 扩展 mysql5.5源码编译安装

11.1 LAMP架构介绍11.2 MySQL.MariaDB介绍11.3/11.4/11.5 MySQL安装扩展mysql5.5源码编译安装   http://www.aminglinux.com/bbs/thread-1059-1-1.html mysql5.7二进制包安装(变化较大)  http://www.apelearn.com/bbs/thread-10105-1-1.html =====================================================

11.1 LAMP架构介绍 11.2 MySQL、MariaDB介绍及安装

11.1 LAMP架构介绍 11.2 MySQL.MariaDB介绍 11.3/11.4/11.5 MySQL安装 wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz 原文地址:http://blog.51cto.com/12058686/2073311