6. Multithreaded singleton mode

Time:2019-10-31

 

 

 

The original singleton mode is no longer applicable in multithreaded environment, so what should I do?

Answer:

(1) single instance mode of thread safety with double locking

 

 

(2) static initialization singleton mode

 

 

Direct instantiationSingletonObject inGetInstanceMethod_Instanceobject

 

We know that static members are initialized only once, that is to say_InstanceThe class is instantiated the first time it is accessed, soSingletonWhen the class is first accessed, it is doneSingletonClass is instantiated only once. In this way, the singleton mode in multithreaded environment can also be guaranteed.

 

 

Correction: Please note that the parameterless constructor of singleton in the figure should be declared as private!!!!!!!!!!!!!!!!!!!

Recommended Today

gcasDtServ.exe What process and what function does gcasdtserv process query

Process file: gcasdtserv or gcasDtServ.exeProcess name: Microsoft antispyware server processProcess category: processes with security risks English Description:gcasDtServ.exe is a process belonging to the Giant/Microsoft AntiSpyware product. This program is important for the stable and secure running of your computer and should not be terminated. Chinese reference:gcasDtServ.exe It’s about giant / Microsoft antispyware.Produced by giant / […]