Pthread Stubs in C Library
pthread_key_create(3T)
pthread_getspecific(3T)
pthread_setspecific(3T)
ptherad_key_delete(3T)
pthread_exit(3T)
Calls to the stub,
pthread_self(3T) always returns 1
pthread_equal(arg1, arg2) returns (arg1 == arg2)
pthread_create(3T) and pthread_attr_init(3T) returns ENOSYS.
Link Order problems:
An application may inadvertently pick up the stubs present in libc when it is intended to use the real pthread APIs, or
cma APIs. These are link order problems. An application that needs cma behavior must link to libcma and must do so
in the supported link order, i.e. the link line should only be shared and not contain -lc before -lcma.
So long as this condition is met, the correct cma functions will be referenced. Similarly, a multithreaded application
that needs pthread library behavior must link to libpthread and must do so in a supported link order, and only use
shared libc and libpthread.
Examples of Potential link order problems:
Example 1
The applications or any library linked that will resolve pthread/cma calls to the stubs must be built without -lpthread
or -lcma on the link line. If you specify -lc before -lpthread, your application will use the pthread stubs in libc, but
other problems may occur 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