C + + implements Singleton single case mode in various ways __c++

Source: Internet
Author: User

Topic: Design a class that can only generate an instance of the class.

(1) Solution one: only applicable to single-threaded environment

Since a requirement can only generate one instance, we must set the constructor as a private function to prevent others from creating the instance. We can define a static instance and create that instance when needed.

public class singleton1{

  private Singleton1 () {
  
  }
  
  private static Singleton1 instance = NULL;
  
  public static Singleton1 funcinstance{
  
    get{
    
      if (instance = = NULL) {
      
        instance = new Singleton1 ();
      }
     
      return instance;
      
    }
  }

  

In the Singleton1 static method Funcinstance (), the preceding code creates an instance only when instance is null to avoid duplicate creation, and defines the constructor as a private function so that only one instance is created.



(2) Solution two: Although can be in a multithreaded environment can work but not high efficiency

The code in the solution one works fine in a single thread, but it is problematic in multi-threaded situations. Assuming that two threads are running at the same time to the IF statement that determines whether instance is null, and instance does not, then two threads will create an instance that will no longer satisfy the requirement of singleton mode.

In order to ensure that we can only get one instance of a type in a multithreaded environment, we need to add a synchronous lock. Implemented as follows:

public class singleton2{

  private Singleton2 () {
  
  }
  
  private static readonly Object syncobj = new Object ();
  
  private static Singleton2 instance = NULL;
  
  public static Singleton2 funcinstance{
  
    get{
      
      Lock (sync) {
      
        if (instance = = NULL) {
          
          instance = new Singleton2 ();
        }
      
      return instance;
      
    }
  }

  

We're still fake. There are two threads and you want to create an instance. Since only one thread can get a synchronous lock at a time, the second thread can only wait when the first thread is locked. When the first thread discovers that an instance has not yet been created, he creates an instance. Then the first thread releases the sync lock, at which point the second thread can add a sync lock and run the next code. This time because the instance is already created by the first thread, the second thread does not create the instance repeatedly. But lock is a more time-consuming operation and we should try to avoid it.


(3) Solution three: plus the synchronization lock before and after two times to determine whether the instance has existed

We just need to lock the instance before it is created to ensure that only one thread creates the instance. When the instance has been created, there is no need to lock the operation. The code implementation is as follows:

public class singleton3{

  private Singleton3 () {
  
  }
  
  private static readonly Object syncobj = new Object ();
  
  private static Singleton3 instance = NULL;
  
  public static Singleton3 funcinstance{
  
    get{
      
      if (instance = = null) {
      
        lock (sync) {
          
          if (instance = null) {
            
            instance = new Singleton3 ();
      
      }} return instance;
      
    }
  }

  

A lock operation is required in Singleton3 only if instance is null or not created. When instance has been created, there is no need to lock it. Because only the first time instance is null, it is only necessary to lock the first attempt to create an instance, so that Singleton3 time efficiency is better than Singleton2.


GitHub home: https://github.com/chenyufeng1991. You are welcome to visit.





















Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.