C ++ implements the singleton mode and Multithreading

Source: Internet
Author: User

Http://buptdtt.blog.51cto.com/2369962/975101

  1. // Singleton. h
  2. # Ifndef _ singleton_h _
  3. # DEFINE _ singleton_h _
  4. # Include <iostream>
  5. # Include <pthread. h>
  6. Using namespace STD;
  7.  
  8. Class locker
  9. {
  10. Public:
  11. Inline locker () {pthread_mutex_init (& mutex, null );}
  12. Inline ~ Locker () {pthread_mutex_destroy (& mutex );}
  13. Inline void lock () {pthread_mutex_lock (& mutex );}
  14. Inline void unlock () {pthread_mutex_unlock (& mutex );}
  15. PRIVATE:
  16. Pthread_mutex_t mutex;
  17. };
  18. Class Singleton
  19. {
  20. Public:
  21. Static Singleton * instance ();
  22.  
  23. PRIVATE:
  24. Singleton ();
  25. Static Singleton * m_pinstance;
  26. Class Garbo // Delete the singleton Instance Object
     
  27. {
  28. Public:
  29. ~ Garbo ()
  30. {
  31. If (singleton: m_pinstance)
  32. {
  33. Delete singleton: m_pinstance;
  34. }
  35. }
  36. };
  37. Static Garbo GB; // At the end of the program, the system will call its destructor
     
  38.  
  39. };
  40. # Endif //~ _ Singleton_h _
  41.  
  42.  
  43.  
  44.  
  45. // Singleton. cpp
  46. # Include "Singleton. H"
  47. # Include <iostream>
  48. Using namespace STD;
  49.  
  50.  
  51. Singleton * singleton: m_pinstance = 0;
  52. Singleton: Singleton ()
  53. {
  54. Cout <"Singleton..." <Endl;
  55. }
  56. Singleton * singleton: instance ()
  57. {
  58. If (null = m_pinstance)
  59. {
  60. Locker llock;
  61. Llock. Lock ();
  62. If (null = m_pinstance)
  63. {
  64. M_pinstance = new Singleton ();
  65. }
  66. Llock. Unlock ();
  67. }
  68. Return m_pinstance;
  69.  
  70. }
  71.  
  72. // Main. cpp
  73. # Include "Singleton. H"
  74. # Include <iostream>
  75. Using namespace STD;
  76. Int main (INT argc, char * argv [])
  77. {
  78. Singleton * SGN = singleton: instance ();
  79. Return 0;
  80. }
  81. Declare the constructor as private to prevent it from being instantiated. Use a static member variable and a static function to construct a unique object. New Space is added to static functions. Therefore, the built-in destructor of member objects are used to release the memory. To ensure multi-threaded security, lock objects before they are created, and then unlock objects after they are created.

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.