Operating System - HP-UX
1837007 Members
1799 Online
110111 Solutions
New Discussion

Shared libraries, threads and gethostbyname

 
Stanley Lewis_1
New Member

Shared libraries, threads and gethostbyname

Hi!

I'm trying to figure out why this code:

--------------------------------------
#include
#include
#include

extern "C"
{
void* test_thr(void* args)
{
cout << "-------------------------------" << endl;
cout << "Calling gethostbyname()" << endl;
hostent* host = gethostbyname("localhost");
cout << "host address is: " << host->h_name << endl;
cout << "errno is: " << errno << endl;
cout << "-------------------------------" << endl;
}
}

int do_test( void )
{
pthread_t tid;
pthread_create(&tid, NULL, test_thr, NULL);
pthread_join(tid, NULL);
}
--------------------------------------

Blocks on the gethostbyname() call when the above is put into a shared library and do_test() is called. Is there anything inherantly wrong with calling hostname lookup API's from the context of a thread created from a shared library that's been loaded into the main executable via shl_load()?

Thanks!

5 REPLIES 5
Mike Stroyan
Honored Contributor

Re: Shared libraries, threads and gethostbyname

It works fine for me in this small test program. Note that pthread should be the first system header included. All the files in the program and shared library should be compiled with aCC -mt or equivalent defines for threadsafe.
Stanley Lewis_1
New Member

Re: Shared libraries, threads and gethostbyname

Hmmm... That does work better.

I can't tell, but I think my application is getting into a situation like the attached archive. In this case, I've got a global object that in turns makes the "do_test()" call. In this case the gethostbyname() call blocks...
Stanley Lewis_1
New Member

Re: Shared libraries, threads and gethostbyname

BTW thanks for the code!
Mike Stroyan
Honored Contributor

Re: Shared libraries, threads and gethostbyname

That attachment is not readable.
Stanley Lewis_1
New Member

Re: Shared libraries, threads and gethostbyname

I guess the forum doesn't like .tar.gz as an extension? Maybe this one will work. Thanks!