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