pthread_stubs.5 (2010 09)
p
pthread_stubs(5) pthread_stubs(5)
Example 1
An application or any library linked, that requires pthread/cma calls to resolve to the pthread stubs in
libc must be built without -lpthread or
-lcma on the link line.
If
-lc is specified before -lpthread
or -lcma on the link line, pthread/cma calls resolve to pthread
stubs in
libc. This may lead to problems as given in the examples below:
$ cat thread.c
#include <pthread.h>
#include <stdio.h>
void *thread_nothing(void *p)
{
printf("Success\n");
}
int main()
{
int err;
pthread_t thrid;
err = pthread_create(&thrid, (pthread_attr_t *) NULL, thread_nothing,
(void *) NULL);
sleep(1);
if (err)
{
printf("Error\n");
return err;
}
}
$ cc thread.c -lc -lpthread
$ a.out
Error
$ chatr a.out
a.out:
shared executable
shared library dynamic path search:
SHLIB_PATH disabled second
embedded path disabled first Not Defined
shared library list:
dynamic /usr/lib/libc.2 <- libc before libpthread
dynamic /usr/lib/libpthread.1
shared library binding:
deferred
global hash table disabled ...
Solution for Example 1
For threaded applications, run the executable with environment variable
LD_PRELOAD set to the
libpthread library or link the executable with -lpthread:
$ LD_PRELOAD="/usr/lib/libpthread.1" a.out
Success
$ cc thread.c -lpthread
$ a.out
Success
$ chatr a.out
a.out:
shared executable
shared library dynamic path search:
SHLIB_PATH disabled second
embedded path disabled first Not Defined
shared library list:
HP-UX 11i Version 3: September 2010 − 3 − Hewlett-Packard Company 3