Using thread-safe C library functions.

Using thread-safe C library functions.

Post by Karl-Jose Fille » Fri, 16 Feb 2001 18:18:13




> Hi:

> I'm porting an application to POSIX threads (actually, I'm porting it
> to use an underlying library which handles event management, so I
> don't use the pthreads interface directly) so -- among other things,
> to be sure -- I'll need to change the application to use the *_r
> functions.  The application runs on AIX, Solaris, HP-UX and NT.
> (I don't care about NT at the moment.)  What do I need to do on
> each platform to ensure that we use these functions, and -- more
> importantly -- that the application is MT-safe?

> First, I believe we need to use -D_REENTRANT and, of course,
> modify all non-MT-safe C library calls to use the *_r interface
> instead.

The very first statement should be a

    #include <pthread.h>

As this overwrits a lot of routines

We had a lot of problems with pthreads under 10.20 and a fair part
went way after only using this include EVERYWHERE

there is a very nice HP manual "Programming with threads", which
gives a lot of useful hints and do/donts (e.g. functions to avoid ...)

Jose

Quote:

> Then:

> AIX:
>         Link against -c_r?

> Solaris:
>         Doesn't look like you need special linkage on Solaris to get
>         the _r functions.  Is that correct.

> HP-UX:
>         On HP-UX 11.00 libc_r.sl is a symbolic link to libc.sl.  Does
>         one need to link against -lc_r as well?  What's the purpose of
>         the link?

> Most if not all of the data in each thread sits on the stack.
> (The global data is accessed via and protected by the underlying
> library.)  So other than checking whether our own functions rely
> on static buffers, I don't think I have to change much else.

> I'm particularly interested in what one needs to do in order to
> use the *_r interface.  It'd be nice to be able to use the *_r
> interface without any side effects (e.g., automatically being
> linked against -lpthread, or something awful like that).  Since
> the library this application is built on allows you to use user-space
> threads or POSIX threads, I'd like to be able to build for either
> with minimal hassle.

> Regards,

> Nick Dronen

  josefiller.vcf
< 1K Download
 
 
 

Using thread-safe C library functions.

Post by Hamish Marso » Fri, 16 Feb 2001 21:27:17



Quote:> Hi:
> I'm porting an application to POSIX threads (actually, I'm porting it
> to use an underlying library which handles event management, so I
> don't use the pthreads interface directly) so -- among other things,
> to be sure -- I'll need to change the application to use the *_r
> functions.  The application runs on AIX, Solaris, HP-UX and NT.
> (I don't care about NT at the moment.)  What do I need to do on
> each platform to ensure that we use these functions, and -- more
> importantly -- that the application is MT-safe?
> First, I believe we need to use -D_REENTRANT and, of course,
> modify all non-MT-safe C library calls to use the *_r interface
> instead.
> Then:
> AIX:
>    Link against -c_r?

This is automatic if you compile with cc_r instead of cc. It has everything
setup for you.
You still need to #include <pthreads.h> as the first include file though so
that some thing (e.g. errno) are set to their thread-safe equivalent but there
seems to be no automatic way to make sure that you're using the thread-safe
version of library calls (e.g. gethostbyname vs gethostbyname_r) and the
system will let you call non-thread safe libraries without warnings or errors
which can be a bit of a bummer... And hard to debug...

--

 
 
 

1. using thread-safe library code without -lpthreads

Hi there

do have any experinces whith the use of gethotbyname_r ?

Maybe i'm too dumb, but i don't get any good results, which
i was having with the use of gethostbyname().

This is my current usage:

from the function parameters is defined char *hostname.

struct hostent *host;
struct hostent **hstres;
char *buffer;
int *myerrno;
int rc2;

rc2=gethostbyname_r(hostname,host,buffer,sizeof(buffer),hstres,myerrno);

The only parameter which is set is the hostname. And i'm not getting
any good results whith it.

it works "fine" when i'm using it with the old func.
But this giving me headache when i have more then one thread using
the function.

when i'm trying to access host->h_addr i'm getting a memory access
failure.

what have i to do that i have the correct values, which i normaly
have from host->h_addr and host->h_lenght ?  

host = gethostbyname(hostname)
there i can then access the hostent fields normal.
to copy the adress of the host.

see ya
Thomas

2. Modem Won't Stay Connected

3. List of non-thread-safe SunOS library functions?

4. How to compile a loadable module for an older kernel version!???

5. Documentation for thread-safe network lookup functions.

6. Paging errors during Linux ramdisk install

7. Thread-safe functions

8. ls -l

9. Are the dl* functions thread-safe?

10. Thread-safe gethostby* functions in AIX 4.3.3.

11. Making libraries thread-safe

12. Making a library thread-safe

13. Thread-safe C libraries in Solaris?