# Cannot get pthread_getspecific to work on FreeBSD



## ph7 (Nov 2, 2011)

Hi,

I have trouble setting a thread specific variable with the pthread API on FreeBSD (I am resorting to pthread because Clang does not seem to support gcc's __thread specifier)

A sample program like:


```
#include <pthread.h>
#include <stdio.h>

pthread_key_t   tlsKey = 0;

int main(int argc, char **argv)
{
  int tls;

  pthread_key_create(&tlsKey, NULL);
  pthread_setspecific(tlsKey, 24);
  tls = pthread_getspecific(tlsKey);
  printf("Got %d\n", tls);
  return 0;
}
```

would print 24 on Linux and OS X, but 0 on FreeBSD (8.2-RELEASE-p3)

I am sure that I am misusing the pthread API, but I could use some help in identifying the problem.

Thanks in advance,
- Philippe


----------



## oops (Nov 3, 2011)

add -pthread to cflags as libc provides the stubs, see gen/_pthread_stubs.c


----------



## ph7 (Nov 3, 2011)

Thanks a lot. This was the the problem.


----------

