Previous time using C + + for project development, you need to load the global profile according to the profile path and provide a unique access point externally. In the face of such a demand, it is natural to think of using singleton mode to create a singleton configuration object for external invocation. I started out with a singleton class from boost, but unfortunately, the Singleton class in boost seems to use only the parameterless class constructor, and I want to use the configuration file path as the constructor parameter for a singleton configuration object, and this is the opportunity to implement a singleton class yourself using C + +.
1. Thread-safe C + + Singleton class
Implementing a thread-safe C + + Singleton class, mainly to achieve the following: 1) The constructor is privatized, that is, the constructor, copy constructor, and copy constructor are defined as private. The constructor is privatized to prevent the class object from being defined outside the class; The copy constructor is privatized to prevent multiple instances of copy behavior, and the copy constructor is privatized to prevent multiple instances from being assigned. 2) provide a static global access point for external calls Access 3) through the locking mechanism or static initialization, to ensure the security of multi-threaded access to singleton objects. The procedure is as follows:
Listing 1: A singleton class Config.h
1 #ifndef _config_h_2 #define_config_h_3#include <windows.h>4#include <iostream>5 using namespacestd;6 classConfig7 {8 Private://1. Constructor Private9 Config ()Ten { OneM_path ="config.cfg"; A loadglobalconfig (); - } -Config (stringpath): M_path (path) the { - loadglobalconfig (); - } -Config (ConstConfig &);//copy constructors are not implemented to prevent copies from producing multiple instances +Config &operator= (ConstConfig &);//the copy constructor is not implemented to prevent multiple instances from being assigned - Public: + StaticConfig * getinstance ()//2. Provide a global access point A { at StaticConfig M_singletonconfig;//3.c++11 ensures multithreading security, freeing up resources when programs exit - return&M_singletonconfig; - } - voidLoadglobalconfig () - { - //std::cout<< "111" <<std::endl; in //Sleep (1000); //Sleep 1000ms - //std::cout<< "222" <<std::endl; to //load configuration file ... + } - Private: the stringM_path;//path to the configuration file * }; $ #endif //_config_h_
2. Static Thread safety test
As mentioned earlier, C++11 guarantees the thread security of the static object when it executes the constructor initialization. For this attribute of the static variable in this c++11, I have done an experiment that verifies the constructor thread security of the static class object. Undo the comments for the 28-30 lines of code in Listing 1, and execute main.cpp. The main.cpp code is as follows:
List 2:main.cpp
1#include"config.h"2#include <thread>3 #defineThread_num 24 voidgteststatic ()5 {6Config *pconf=config::getinstance ();7 }8 intMain ()9 {Ten Std::thread Threadarray[thread_num]; One for(intI=0; i<thread_num;i++) A { -Threadarray[i] = Std::thread (>eststatic); - } the for(inti =0; i < Thread_num; i++) - { -Threadarray[i].join ();//The main thread waits for all threads to end - } + return 0; -}
Listing 3: Experimental results
1 Output: 2 111 3 222
As you can see from this experiment, one thread sleeps 1ms while executing the class's constructor, and another thread waits, so the constructor for the static object is actually executed only once. Therefore, C++11 does ensure that the static object constructor initializes the multithreaded security.
C + +: Self-paced C + + Singleton class for thread-safe