1 2 #include <pthread.h> 3 #include <stdio.h> 4 #include <unistd.h> 5 6 int __thread i; 7 8 void * 9 foo1(void *arg) 10 { 11 printf("thread %p, &i = %p\n", pthread_self(), &i); 12 for (i = 0; i < 10; i++) { 13 printf("thread %p, i = %d\n", pthread_self(), i); 14 sleep(1); 15 } 16 return (NULL); 17 } 18 19 void * 20 foo2(void *arg) 21 { 22 printf("thread %p, &i = %p\n", pthread_self(), &i); 23 for (i = 10; i > 0; i--) { 24 printf("thread %p, i = %d\n", pthread_self(), i); 25 sleep(1); 26 } 27 return (NULL); 28 } 29 30 int 31 main(int argc, char** argv) 32 { 33 pthread_t t1, t2; 34 35 pthread_create(&t1, 0, foo1, 0); 36 pthread_create(&t2, 0, foo2, 0); 37 pthread_join(t1, 0); 38 pthread_join(t2, 0); 39 40 return (0); 41 } 42