C 单例模式

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使用的正确性。

posted @ 2014-11-26 19:51  aspirant  阅读(1957)  评论(1编辑  收藏  举报