首页 > 解决方案 > pthread_once() 是否支持“链接”另一个 init_routine?

问题描述

是否从支持的 init_routine 中调用另一个 pthread_once(使用不同的 once_control 和 init_routine)?

例如,

static pthread_once_t gl_ctl_one = PTHREAD_ONCE_INIT;
static pthread_once_t gl_ctl_two = PTHREAD_ONCE_INIT;

int main (int argc, char *argv[])

{ 
   pthread_once(&gl_ctl_one, rtn_one);
};

void rtn_one() 
{ 
   printf("rtn_one\n");

   /**********************************************/
   /* Call the second init if needed             */
   /**********************************************/
   pthread_once(&gl_ctl_two, rtn_two);

   return;
} 

void rtn_two()
{ 
   printf("rtn_two\n");

   return;
} 

我实际上是在尝试初始化两个不同的“.so”“包”,所以我需要有两个单独的初始化例程。(我不能把它们合二为一,可能涉及也可能不涉及多个进程和线程)。

rtn_one() 实际上在一个.so 中,而 rtn_two() 在另一个.so 中。

在http://pubs.opengroup.org/onlinepubs/9699919799/上查看 pthread_once 的文档让我有点困惑。他们提到“递归”是一个坏主意,但仅提及尝试使用相同的 once_control (我对他们的推理有点困惑):

“如果 init_routine 以相同的 one_control 递归调用 pthread_once(),则递归调用将不会调用指定的 init_routine,因此指定的 init_routine 将不会完成,因此对 pthread_once() 的递归调用将不会返回。”

就我而言,我使用的是 Centos 7.5 linux,它似乎可以工作(我不确定 pthread_once() 中的内部互斥锁是否会导致问题)。

但是,我不确定这是否是承诺的行为。

标签: linuxpthreads

解决方案


推荐阅读