|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <pthread.h> |
| 4 | +#include <unistd.h> |
| 5 | +#include <sys/types.h> |
| 6 | + |
| 7 | +static pthread_key_t me_key; |
| 8 | +static pthread_key_t you_key; |
| 9 | +static pthread_key_t them_key; |
| 10 | + |
| 11 | +static void *tf(void *arg) |
| 12 | +{ |
| 13 | + pthread_setspecific(me_key, (const void *) 200); |
| 14 | + printf("me_key in thread (%p): %u, me value in thread: %zu\n", &me_key, me_key, (size_t) pthread_getspecific(me_key)); |
| 15 | + printf("you_key in thread (%p): %u, you value in thread: %zu\n", &you_key, you_key, (size_t) pthread_getspecific(you_key)); |
| 16 | + printf("them_key in thread (%p): %u, them value in thread: %zu\n", &them_key, them_key, (size_t) pthread_getspecific(them_key)); |
| 17 | + |
| 18 | + return NULL; |
| 19 | +} |
| 20 | + |
| 21 | +int main(void) |
| 22 | +{ |
| 23 | + pthread_t new; |
| 24 | + int err; |
| 25 | + |
| 26 | + pthread_key_create(&me_key, NULL); |
| 27 | + pthread_key_create(&you_key, NULL); |
| 28 | + pthread_key_create(&them_key, NULL); |
| 29 | + |
| 30 | + pthread_setspecific(me_key, (const void *) 100); |
| 31 | + pthread_setspecific(you_key, (const void *) 999); |
| 32 | + pthread_setspecific(them_key, (const void *) 3); |
| 33 | + printf("me_key before create (%p): %u, me value before create: %zu\n", &me_key, me_key, (size_t) pthread_getspecific(me_key)); |
| 34 | + printf("you_key before create (%p): %u, me value before create: %zu\n", &you_key, you_key, (size_t) pthread_getspecific(you_key)); |
| 35 | + printf("them_key before create (%p): %u, me value before create: %zu\n", &them_key, them_key, (size_t) pthread_getspecific(them_key)); |
| 36 | + |
| 37 | + err = pthread_create(&new, NULL, tf, NULL); |
| 38 | + if (err < 0) { |
| 39 | + perror("pthread_create"); |
| 40 | + exit(EXIT_FAILURE); |
| 41 | + } |
| 42 | + |
| 43 | + printf("me_key after create before join (%p): %u, me value after create before join: %zu\n", &me_key, me_key, (size_t) pthread_getspecific(me_key)); |
| 44 | + printf("you_key after create before join (%p): %u, you value after create before join: %zu\n", &you_key, you_key, (size_t) pthread_getspecific(you_key)); |
| 45 | + printf("them_key after create before join (%p): %u, them value beafter create before join: %zu\n", &them_key, them_key, (size_t) pthread_getspecific(them_key)); |
| 46 | + pthread_join(new, NULL); |
| 47 | + printf("me_key after join (%p): %u, me value after join: %zu\n", &me_key, me_key, (size_t) pthread_getspecific(me_key)); |
| 48 | + printf("you_key after join (%p): %u, you value after join: %zu\n", &you_key, you_key, (size_t) pthread_getspecific(you_key)); |
| 49 | + printf("them_key after join (%p): %u, them value after join: %zu\n", &them_key, them_key, (size_t) pthread_getspecific(them_key)); |
| 50 | + |
| 51 | + /* This is really not required, but we call it for API completeness. */ |
| 52 | + pthread_key_delete(me_key); |
| 53 | + pthread_key_delete(you_key); |
| 54 | + pthread_key_delete(them_key); |
| 55 | + |
| 56 | + return 0; |
| 57 | +} |
0 commit comments