使用 pthread_once(3C),可以在首次调用 pthread_once 时调用初始化例程。以后调用 pthread_once() 将不起作用。
int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));
#include <pthread.h> pthread_once_t once_control = PTHREAD_ONCE_INIT; int ret; ret = pthread_once(&once_control, init_routine);
once_control 参数用来确定是否已调用相关的初始化例程。
pthread_once() 在成功完成之后返回零。其他任何返回值都表示出现了错误。如果出现以下情况,pthread_once() 将失败并返回相应的值。
EINVAL
描述:once_control 或 init_routine 是 NULL。