/* $Id: rtld_dlsym_tls.c,v 1.1 2012/03/29 10:23:11 kostik Exp kostik $ */ #include #include #include #include #include #include static const int nthreads = 10; __thread int tno; struct test_thread_arg { int tno; }; static void * test_thread(void *arg) { struct test_thread_arg *tta; void *tno_ptr; tta = arg; tno = tta->tno; printf("thread %d: tno %p %d\n", tta->tno, &tno, tno); tno_ptr = dlsym(NULL, "tno"); if (tno_ptr == NULL) errx(1, "Cannot resolve symbol tno: %s", dlerror()); printf("thread %d: tno-> %p %d\n", tta->tno, tno_ptr, *(int *)tno_ptr); free(arg); return (NULL); } int main(void) { struct test_thread_arg *tta; pthread_t threads[nthreads]; int error, i; tno = 0; for (i = 0; i < nthreads; i++) { tta = calloc(1, sizeof(*tta)); tta->tno = i; error = pthread_create(&threads[i], NULL, test_thread, tta); if (error != 0) errc(1, error, "pthread_create %d", i); } for (i = 0; i < nthreads; i++) { error = pthread_join(threads[i], NULL); if (error != 0) errc(1, error, "pthread_join %d", i); } return (0); }