C getaddrinfo_是线程安全的吗?

C getaddrinfo_是线程安全的吗?,c,linux,multithreading,posix,C,Linux,Multithreading,Posix,我想使用getaddrinfo_函数。这个方法是线程安全的吗? 在给出的手册页示例中,使用全局列表解析主机名。 如果我在用户空间中操作该列表,那么它安全吗? 伪代码如下: static struct gaicb **reqs =NULL; // global list of hostname to resolve. addToList() { ret = getaddrinfo_a( GAI_NOWAIT, &reqs[nreqs_

我想使用getaddrinfo_函数。这个方法是线程安全的吗? 在给出的手册页示例中,使用全局列表解析主机名。 如果我在用户空间中操作该列表,那么它安全吗? 伪代码如下:

static struct gaicb **reqs =NULL; // global list of hostname to resolve.

addToList() {
   ret =
      getaddrinfo_a(
         GAI_NOWAIT,
         &reqs[nreqs_base], 
         nreqs - nreqs_base,
         NULL ); // enque hostname queue.
}

//another thread  method
dequeu_list( int i ) {
   struct gaicb * result = reqs[i] ;
   reqs[i] = NULL;
}
是的,请参见:

。。。
int
getaddrinfo_a(int模式,结构gaicb*列表[],int ent,结构SIGFENT*sig)
{
...
无法登录
...
/*请求互斥锁*/
pthread_mutex_lock(&uu_gai_请求_mutex);
/*现在我们可以将所有请求排队。因为我们已经获得了
互斥排队函数不需要这样做*/
对于(cnt=0;cnt
它在访问
列表
之前获取互斥锁

无论如何,它类似于线程安全的
getaddrinfo

freeaddrinfo()
getaddrinfo()
函数应是线程安全的


为了始终确保您的函数是线程安全的,您可以创建一个互斥锁,在您即将调用它时将其锁定,在完成调用时将其解锁。感谢您回答这个问题。但是,对于排队互斥锁,它是在另一个线程/内核线程上下文中获取的。我正在删除或处理用户/进程上下文中的相同列表,这不是问题吗?得到答案。我必须为我的列表保护互斥。@user50891此代码在用户模式下运行,而不是在内核中,它是glibc的一部分。如果在异步模式下运行,则会正确创建新线程。该线程具有指向全局列表“reqs”的指针。
...
int
getaddrinfo_a (int mode, struct gaicb *list[], int ent, struct sigevent *sig)
{
...
  no acess to list
...
  /* Request the mutex.  */
  pthread_mutex_lock (&__gai_requests_mutex);

  /* Now we can enqueue all requests.  Since we already acquired the
     mutex the enqueue function need not do this.  */
  for (cnt = 0; cnt < ent; ++cnt)
    if (list[cnt] != NULL)
      {
...