getaddrinfo_a线程安全吗?

use*_*891 2 c linux multithreading posix

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

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;
}
Run Code Online (Sandbox Code Playgroud)

小智 5

是的,请参阅源代码:

...
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)
      {
...
Run Code Online (Sandbox Code Playgroud)

它在访问之前获取互斥锁list.

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

freeaddrinfo()getaddrinfo()功能应是线程安全的.