#include #include #include static pthread_key_t key; static void *check_key(void *dummy) { /* check the initial key value */ printf("PID: %d, TID: %lu, init value: %p\n", getpid(), pthread_self(), pthread_getspecific(key)); pthread_setspecific(key, (void *)0xdeadbeef); sleep(2); return NULL; } int main(int argc, char *argv[]) { pthread_t pth1, pth2; pthread_key_create(&key, NULL); pthread_create(&pth1, NULL, check_key, NULL); /* let the first thread update its TLS */ sleep(1); if (fork() == 0) { /* child */ pthread_create(&pth2, NULL, check_key, NULL); pthread_join(pth2, NULL); } else { pthread_join(pth1, NULL); } return 0; }