Domanda

Sto lavorando in C ++ con VS2008 e Win7.

Mentre l'esame di un programma di stavo seguendo i fili creati, e sembra che gethostbyname () crea un thread per se stessa. Ci può spiegare perché?

su MSDN è dice: "La memoria per la struttura hostent restituito dalla funzione gethostbyname è allocata internamente dal Winsock DLL dalla memoria locale thread."

Questo memoria ingannare visual studio a pensare che è un filo?

EDIT: Sembra che da questo link , e anche dalle mie osservazioni che questo accade anche con la funzione Connect. Credo che questo è un comportamento normale.

Il seguente codice è da MSDN [pagina gethostbyname] e presenta lo stesso comportamento.

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;
}
È stato utile?

Soluzione

Per quanto ne so, blocchi gethostbyname.

WinSock crea spesso alcuni fili helper però.

Altri suggerimenti

No, l'archiviazione thread-local non è correlato alla messa in servizio di un nuovo thread.

Il filo può essere necessaria per problemi filo-affinità con sotto-operazioni di API GetHostByName, come la necessità di utilizzare i callback asincroni senza influenzare re-entrancy del thread chiamante.

O può essere una caratteristica di inizializzazione ritardata WinSock dove è necessario un thread demone necessaria per un sottoinsieme di operazioni WinSock, e questo era il primo API per richiedere la daemon.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top