1 #include2 #include 3 #include 4 5 #define UPBOUND 100 6 7 sem_t sem1; 8 sem_t sem2; 9 10 //int i=0;11 12 void *threadfunc1(void *p)13 {14 int i=0;15 while(i<100)16 {17 sem_wait(&sem1);18 i++;19 printf("111 thread i is %d\n",i);20 sem_post(&sem2);21 }22 return NULL;23 }24 25 26 void *threadfunc2(void *p)27 {28 int i=0;29 while(i<100)30 {31 sem_wait(&sem2);32 i++;33 printf("222 thread i is %d\n",i);34 sem_post(&sem1);35 }36 return NULL;37 }38 39 int main()40 {41 sem_init(&sem1, 0, 1); //线程间共享,初值为142 sem_init(&sem2, 0, 0);43 44 pthread_t tid1=0, tid2=0;45 46 pthread_create(&tid1,NULL,&threadfunc1,NULL);47 pthread_create(&tid2,NULL,&threadfunc2,NULL);48 49 pthread_join(tid1,NULL);50 pthread_join(tid2,NULL);51 52 sem_destroy(&sem1);53 sem_destroy(&sem2);54 55 return 0;56 }