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

来源:互联网 发布:at070tn94数据手册 编辑:程序博客网 时间:2024/04/29 02:12

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 0
原创粉丝点击