Since every compiler has its own version of thread local storage, I ended up creating a macro for it. The only problem now is GCC (with pthreads turned off), which gives me:
"thread-local storage not supported for this target"
Fair enough, given that pthreads are actually turned off in this case. The question is, is there a generic way of detecting this using some macro e.g. #ifdef __GCC_XXX_NO_THREADS_XXX ?
EDIT: See the accepted answer below. Also, here's my lazy solution:
$ touch test.c
$ gcc -E -dM test.c > out.1
$ gcc -pthread -E -dM test.c > out.2
$ diff out.*
28a29
> #define _REENTRANT 1
This is on Mac OS X. I am not sure if it's portable or anything...
Your compile command line either has -lpthread
or not: You could include a -DHAVE_PTHREADS
there as well.
If you really want GCC/ELF specific runtime dectection, you could resort to weak refs:
#include <pthread.h>
extern void *pthread_getspecific(pthread_key_t key) __attribute__ ((weak));
int
main()
{
if (pthread_getspecific)
printf("have pthreads\n");
else
printf("no pthreads\n");
}
Here's what it looks like:
$ gcc -o x x.c
$ ./x
no pthreads
$ gcc -o x x.c -lpthread
$ ./x
have pthreads