2010-06-04 4 views
3

저는 C++에서 VS2008 및 Win7을 사용하고 있습니다.gethostbyname이 (가) 스레드를 만드시겠습니까?

내가 만든 스레드를 따라 가고 있었는데 gethostbyname()이 스레드 자체를 생성하는 것 같습니다. 이유를 설명해 주시겠습니까? MSDN의

는 말한다 : "는 경우 gethostbyname 함수에서 반환하는 hostent 구조에 대한 메모리가 스레드 로컬 저장소에서 윈속 ​​DLL에 의해 내부적으로 할당됩니다."

그것이 인 생각으로이 메모리 바보 비주얼 스튜디오를합니까 실?

EDIT : this link에서 보이며 또한 Connect 함수에서도 이와 같은 현상이 발생합니다. 나는 이것이 정상적인 행동이라고 생각한다.

아래 코드는 msdn [gethostbyname 페이지]에서 가져온 것으로 동일 동작을 나타냅니다.

int main(int argc, char **argv)  
{  
    //----------------------------------------- 
    // Declare and initialize variables 
    WSADATA wsaData; 
    int iResult; 

    DWORD dwError; 
    int i = 0; 

    struct hostent *remoteHost; 
    char *host_name; 
    struct in_addr addr; 

    char **pAlias; 

    // Validate the parameters 
    if (argc != 2) { 
     printf("usage: %s hostname\n", argv[0]); 
     printf(" to return the IP addresses for the host\n"); 
     printf("  %s www.contoso.com\n", argv[0]); 
     printf(" or\n"); 
     printf("  %s IPv4string\n", argv[0]); 
     printf(" to return an IPv4 binary address for an IPv4string\n"); 
     printf("  %s 127.0.0.1\n", argv[0]); 
     return 1; 
    } 
    // Initialize Winsock 
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); 
    if (iResult != 0) { 
     printf("WSAStartup failed: %d\n", iResult); 
     return 1; 
    } 

    host_name = argv[1]; 

    printf("Calling gethostbyname with %s\n", host_name); 
    remoteHost = gethostbyname(host_name); 

    if (remoteHost == NULL) { 
     dwError = WSAGetLastError(); 
     if (dwError != 0) { 
      if (dwError == WSAHOST_NOT_FOUND) { 
       printf("Host not found\n"); 
       return 1; 
      } else if (dwError == WSANO_DATA) { 
       printf("No data record found\n"); 
       return 1; 
      } else { 
       printf("Function failed with error: %ld\n", dwError); 
       return 1; 
      } 
     } 
    } else { 
     printf("Function returned:\n"); 
     printf("\tOfficial name: %s\n", remoteHost->h_name); 
     for (pAlias = remoteHost->h_aliases; *pAlias != 0; pAlias++) { 
      printf("\tAlternate name #%d: %s\n", ++i, *pAlias); 
     } 
     printf("\tAddress type: "); 
     switch (remoteHost->h_addrtype) { 
      case AF_INET: 
       printf("AF_INET\n"); 
      break; 
      case AF_NETBIOS: 
       printf("AF_NETBIOS\n"); 
      break; 
      default: 
       printf(" %d\n", remoteHost->h_addrtype); 
      break; 
     } 
     printf("\tAddress length: %d\n", remoteHost->h_length); 

     i = 0; 
     if (remoteHost->h_addrtype == AF_INET) 
     { 
      while (remoteHost->h_addr_list[i] != 0) { 
       addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++]; 
       printf("\tIP Address #%d: %s\n", i, inet_ntoa(addr)); 
      } 
     } 
     else if (remoteHost->h_addrtype == AF_NETBIOS) 
     { 
      printf("NETBIOS address was returned\n"); 
     } 
    } 
    return 0; 
} 
+1

FWIW, VS가 스레드인지 혼란 스러울 때 SysInternals의 ProcessMonitor를 사용하여 확실한 답을 얻을 수 있습니다. –

답변

3

AFAIK, gethostbyname 블록.

WinSock은 종종 도우미 스레드를 생성합니다.

1

아니요, 스레드 로컬 저장소는 새 스레드를 시작한 것과 관련이 없습니다.

호출 스레드의 재진입에 영향을주지 않고 비동기 콜백을 사용해야하는 등 GetHostByName API의 하위 작업에 대한 스레드 유사성 문제로 인해 스레드가 필요할 수 있습니다.

또는 WinSock 작업의 하위 집합에 필요한 데몬 스레드가 필요하고 이것이 데몬을 요구하는 첫 번째 API 인 WinSock의 지연 초기화 기능 일 수 있습니다.