pthread_once函数

一句话总结:保证仅初始化一次,线程安全,比用信号量效率更高。

API:

pthread_once_t once_control = PTHREAD_ONCE_INIT;
int pthread_once(pthread_once_t* once_control, void (*init_routine)(void))


简例:

pthread_once_t m_once_control = PTHREAD_ONCE_INIT;

T m_instance;


static T& instance()

{

    pthread_once(&m_once_control, &init)

}


static void init()

{

    m_instance = new T();

    atexit(destory);

}


static void destory(void)

{

    delete m_instance;

}


你可能感兴趣的:(pthread_once函数)