This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: pthreads and sockets - Cannot register window class error


> >> void test()
> >> {
> >>     /* go find out about the desired host machine */
> >>     struct hostent *he = gethostbyname(HOST);
> >>     if (he == 0) {
> >>         perror("gethostbyname");
> >>         exit(1);
> >>     }
> >
> >Just a wild guess, but gethostbyname() is probably not reentrant and
> >can't be called from threads like that.

Christopher Faylor wrote:
> Unless HOST is a numeric IP address, gethostbyname should be properly
> reentrant.  The only time gethostbyname is not thread safe is when it is
> resolving a numeric IP.


--- GetHostByNameTest.cpp ------------------------------------------
#include <iostream>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>
#include <assert.h>

using std::cout;
using std::endl;


const char *HOST    = "example.org";


void test()
{
    for(int i=0; i<100; ++i)
    {
        /* go find out about the desired host machine */
        struct hostent *he = gethostbyname(HOST);
        if(he == 0)
        {
            perror("gethostbyname");
            exit(1);
        }
        assert( he->h_addrtype == AF_INET );
        assert( he->h_addr_list[0] );
    }
}


void *task(void *arg)
{
    test();
    return NULL;
}


int main()
{
    const int threads = 70;
    pthread_t threadTable[threads];

    for(int i=0; i<threads; ++i)
    {
        if(pthread_create(&threadTable[i], NULL, task, (void *)(i+1)) !=
0)
        {
            cout << "pthread_create() error" << endl;
            abort();
        }
    }

    for(int i=0; i<threads; ++i)
    {
        pthread_join( threadTable[i], NULL );        
    }

    return 0;
}
--- GetHostByNameTest.cpp ------------------------------------------


> $ g++ GetHostByNameTest.cpp -lpthread -o GetHostByNameTest.exe && ./GetHostByNameTest.exe
>
> $ g++ GetHostByNameTest.cpp -lpthread -o GetHostByNameTest.exe && ./GetHostByNameTest.exe
> 
> $ g++ GetHostByNameTest.cpp -lpthread -o GetHostByNameTest.exe && ./GetHostByNameTest.exe
> 
> $ g++ GetHostByNameTest.cpp -lpthread -o GetHostByNameTest.exe && ./GetHostByNameTest.exe
> 
> $ g++ GetHostByNameTest.cpp -lpthread -o GetHostByNameTest.exe && ./GetHostByNameTest.exe
> 
> $ g++ GetHostByNameTest.cpp -lpthread -o GetHostByNameTest.exe && ./GetHostByNameTest.exe
> gethostbyname: Operation not permitted
> gethostbynamegethostbynamegethostbynamegethostbynamegethostbyname: : : : : Operation not permittedOperation > not permittedOperation not permittedOperation not permittedOperation not permitted

Win2k SP4 + hotfixes.
Can someone reproduce it?

Best regards,
Jacek.

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]