当前位置:   article > 正文

设计模式之 单例模式_单例模式在嵌入式中的应用

单例模式在嵌入式中的应用

介绍

几个重点:

  • 构造函数为private
  • 提供一个获取单例对象指针的函数
  • 一个静态指针成员存储单例对象

注意:

  • 获取单例对象也可以获取对象引用,但要注意拷贝构造函数和赋值运算符
  • 如果有多线程访问单例,需要注意线程同步

 

UML类图

简单示例:

懒汉和饿汉模式:

  1. #ifndef SIMPLE_SINGLETON_H
  2. #define SIMPLE_SINGLETON_H
  3. #include <iostream>
  4. using namespace std;
  5. /**
  6. * @brief The LazySingleton class
  7. * 懒汉模式
  8. */
  9. class LazySingleton
  10. {
  11. public:
  12. static LazySingleton *getInstance()
  13. {
  14. if( m_instance == nullptr)
  15. m_instance = new LazySingleton;
  16. return m_instance;
  17. }
  18. static void freeInstance()
  19. {
  20. if( m_instance != nullptr )
  21. delete m_instance;
  22. m_instance = nullptr;
  23. }
  24. private:
  25. LazySingleton()
  26. {
  27. cout<<" LazySingleton 构造函数执行"<<endl;
  28. }
  29. static LazySingleton *m_instance ;
  30. };
  31. LazySingleton *LazySingleton::m_instance = nullptr;
  32. /**
  33. * @brief The HungrySingleton class
  34. * 饿汉模式,一般用不到
  35. */
  36. class HungrySingleton
  37. {
  38. public:
  39. static HungrySingleton *getInstance()
  40. {
  41. if( m_instance == nullptr)
  42. m_instance = new HungrySingleton;
  43. return m_instance;
  44. }
  45. static void freeInstance()
  46. {
  47. if( m_instance != nullptr )
  48. delete m_instance;
  49. m_instance = nullptr;
  50. }
  51. private:
  52. HungrySingleton()
  53. {
  54. cout<<" HungrySingleton 构造函数执行"<<endl;
  55. }
  56. static HungrySingleton *m_instance ;
  57. };
  58. HungrySingleton *HungrySingleton::m_instance = new HungrySingleton;
  59. #endif // SIMPLE_SINGLETON_H

懒汉线程安全模式:

  1. #ifndef THREAD_SINGLETON_H
  2. #define THREAD_SINGLETON_H
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <pthread.h>
  8. using namespace std;
  9. void printids(const char *s)
  10. {
  11. pid_t pid;
  12. pthread_t tid;
  13. pid = getpid();
  14. tid = pthread_self();
  15. printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int) pid,
  16. (unsigned int) tid, (unsigned int) tid);
  17. }
  18. class LazySingleton
  19. {
  20. public:
  21. static LazySingleton *getInstance()
  22. {
  23. // if( m_instance == nullptr)
  24. // {
  25. // m_count++;
  26. // m_instance = new LazySingleton;
  27. // }
  28. if( m_instance == nullptr)
  29. {
  30. pthread_mutex_lock(&m_mutex_lock);
  31. if( m_instance == nullptr)
  32. {
  33. m_count++;
  34. m_instance = new LazySingleton;
  35. }
  36. pthread_mutex_unlock(&m_mutex_lock);
  37. }
  38. return m_instance;
  39. }
  40. static void freeInstance()
  41. {
  42. if( m_instance != nullptr )
  43. delete m_instance;
  44. pthread_mutex_destroy(&m_mutex_lock);
  45. m_instance = nullptr;
  46. }
  47. void showMessage()
  48. {
  49. cout<<"LazySingleton message!"<<endl;
  50. }
  51. private:
  52. LazySingleton()
  53. {
  54. cout<<" LazySingleton 构造函数执行开始"<<endl;
  55. sleep(2);
  56. cout<<" LazySingleton 构造函数执行 ["<<m_count<<"] 次"<<endl;
  57. }
  58. static LazySingleton *m_instance ;
  59. static int m_count;
  60. static pthread_mutex_t m_mutex_lock;
  61. };
  62. LazySingleton *LazySingleton::m_instance = nullptr;
  63. int LazySingleton::m_count = 0;
  64. pthread_mutex_t LazySingleton::m_mutex_lock = PTHREAD_MUTEX_INITIALIZER;
  65. void *thr_fn(void *arg)
  66. {
  67. // printids("new thread: ");
  68. LazySingleton::getInstance()->showMessage();
  69. return nullptr;
  70. }
  71. #endif // THREAD_SINGLETON_H

线程安全,调用方式: 

  1. printids("main thread:");
  2. int err;
  3. pthread_t ntid[3];
  4. for (int i=0 ; i<3 ; i++) {
  5. err = pthread_create(&ntid[i], NULL, thr_fn, NULL);
  6. if (err != 0)
  7. printf("can't create thread: %s\n", strerror(err));
  8. }
  9. for (int i=0; i < 3 ; i++) {
  10. pthread_join(ntid[i],NULL);
  11. }

内部静态类(懒汉)方式:

  1. class StackSingleton
  2. {
  3. public:
  4. static StackSingleton *getInstance()
  5. {
  6. //内部静态实例的懒汉模式
  7. pthread_mutex_lock(&m_mutex_lock);
  8. static StackSingleton _instance ;
  9. pthread_mutex_unlock(&m_mutex_lock);
  10. return &_instance;
  11. }
  12. void printS()
  13. {
  14. cout<<"栈方式的单例模式..."<<endl;
  15. }
  16. private:
  17. StackSingleton()
  18. {
  19. cout<<" StackSingleton 构造函数执行"<<endl;
  20. }
  21. static pthread_mutex_t m_mutex_lock;
  22. };
  23. pthread_mutex_t StackSingleton::m_mutex_lock = PTHREAD_MUTEX_INITIALIZER;

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/143884
推荐阅读
相关标签
  

闽ICP备14008679号