Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1316859
  • 博文数量: 244
  • 博客积分: 1039
  • 博客等级: 少尉
  • 技术积分: 1562
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-06 09:40
文章分类

全部博文(244)

文章存档

2021年(2)

2019年(6)

2018年(2)

2014年(1)

2013年(187)

2012年(47)

分类: C/C++

2013-02-04 16:53:00

Singleton模式 

Singleton 模式是设计模式中最为简单、最为常见、最容易实现

Singleton模式典型的结构图为:

typedef int *SINGLETON;   /* 定义自己的类型 */

SINGLETON *singleton = NULL;

SINGLETON *get_singleton(void)

{

    if (NULL == singleton)

    {

        singleton = calloc(1, sizeof(SINGLETON));

        if (NULL == singleton)

        {

            return NULL;

        }

        /*  SINGLETON 初始化 */

    }

    return  singleton;

}

这是一个不安全的方法, 适用于其他线程或者模块在初始化时有序的。

但是,在多个线程或者多模块同时调用时,可能存在不安全情形,多个线程或者模块同时calloc singleton的情况。 为了避免出现这种情况,实现方式如下:

typedef int *SINGLETON;   /* 定义自己的类型 */

SINGLETON *singleton = NULL;

pthread_mutex_t singleton_lock=PTHREAD_MUTEX_INITIALIZER;

SINGLETON *init_singleton(void)

{

    pthread_mutex_lock(&singleton_lock);

    if (NULL == singleton)

    {

        singleton = calloc(1, sizeof(SINGLETON));

        if (NULL == singleton)

        {

            pthread_mutex_unlock(&singleton_lock);

            return NULL;

        }

        /*  SINGLETON 初始化 */

    }

    pthread_mutex_unlock(&singleton_lock);

    return  singleton;

}

单例适用场景: 

  在模块化设计时,应该遵循耦合度越低越好,但是总是存在一些公共的调用函数库或者模块,这些公共的调用函数库或者模块有以下几个特点:

1、需要被其他的模块使用

2、其他模块之间并不知道对方使用会去调用或者初始化

3、公共的调用函数库或者模块却希望在整个系统中初始化一次。

这种模块其实也很常见,

    比如:线程池(整个系统希望只有那么线程,其他模块不能线程)

再比如:内存池,整个系统中只能存在这么一个内存池。

总结:在实际应用中SINGLETON很可能是一个数据结构, 这个结构中必然需要一把锁,来保证对全局变量singleton使用的正确性。

阅读(4014) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~