1.一个公有函数instance(),其余为私有
- pthread_once():
int pthread_once(pthread_once_t *once_control,
void (*init_routine)(void));
保证init_routine指向的函数只执行一次
- init()函数定义如下:
- atexit():
int atexit(void (*function)(void));
the given function to be called at normal process termination
- destroy()函数定义如下:
typedef定义了一个char数组,数组的大小根据 T 类型是否是完整类型来定。
若不是完整类型,则数组大小为-1,在编译阶段报错。
不完整类型举例:
2.测试用例 Singleton_test.cc
- 运行结果:
各个线程都可以访问到这个单例对象,当一个对象对其进行修改后,其它线程也能得知。
参考代码