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

C++11 单例模式

对于C++11来说,单例模式就是这样简单!以下代码是线程安全的,C++11会自动处理初始化竞争,请看代码,两种不同的实现方式。

#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();
}

template <typename T> class Singleton
{
    protected:
        static T& getInstance()
        {
           static T instance;
           return instance;
        }
    protected:
        Singleton(){cout<<"Singleton ctor"<<endl;}

};
class SingletonA final:public Singleton<SingletonA>
{
    friend Singleton<SingletonA>;
    public:
       using Singleton<SingletonA>::getInstance;
    private:
        SingletonA(){cout<<"SingletonA ctor"<<endl;}

};
//这段代码在C++11下保证线程安全,并且是懒式初始化。
//使用 SingletonA::getInstance()即可获取单例对象,禁止继承



接下来,再讨论如何使用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吧。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值