Transferred from: http://blog.csdn.net/baiding1123/article/details/14053957
Write a program, open 3 threads, the IDs of the 3 threads are a, B, C, each thread prints its own ID on the screen 10 times, the output must be displayed in the order of ABC, such as: Abcabc .... recursion in turn.
mutexes and condition variables.
Assuming that there are three threads A, B, c;a threads are first executed, B threads wait for a to finish, C threads wait for B to execute, a thread waits for the C thread to finish, B waits for a,c to wait for b .... And so on, is a loop waiting process, What each thread is going to do: 1. Grab the lock and set the flag to 1 (tell the home to wait for the signal); 2. Receive home signal, immediately set the sign for 0, and print out its own thread number; 3. Determine whether the next home is waiting, determine the home after waiting, immediately send a signal to the next, and then open the lock (20) (Let other threads grab the lock).
There are two places to note: 1. After three threads have been created, three threads are dying waiting for [a etc c,b et a,c etc] so the first thing to do is to manually replace C for a to send a signal, let the program execute. At the end of the 10 cycles of the 2.A thread, the flag flag bit is 0, and the C thread is not finished yet, C to check if the flag of a is 1 and then send a signal to a, so after the end of a cycle, it is set to 1 flag flag.
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<errno.h>#include<pthread.h>#defineLp_times 10pthread_mutex_t Mutex=pthread_mutex_initializer;pthread_cond_t Cond_ab=pthread_cond_initializer;pthread_cond_t COND_BC=pthread_cond_initializer;pthread_cond_t Cond_ca=Pthread_cond_initializer;//MarkintFlag_ab,flag_bc,flag_ca;//the thread waiting for the tag checkvoidTh_condflag_wait (int*flag,pthread_cond_t *cond,pthread_mutex_t *Mutex) { (*flag) =1; Pthread_cond_wait (Cond,mutex); (*flag) =0;}//thread notifications for tag checkingvoidTh_condflag_broadcast (int*flag,pthread_cond_t *cond,pthread_mutex_t *Mutex) { while(! (*flag)) {Pthread_mutex_unlock (mutex); Usleep ( -); Pthread_mutex_lock (mutex); } pthread_cond_broadcast (cond);}void*th_fun_a (void*Arg) { inti =Lp_times; while(i--) {Pthread_mutex_lock (&mutex); //A Wait CTh_condflag_wait (&flag_CA,&cond_CA,&mutex); printf ("a%d:%lx\n",Ten-i,pthread_self ()); //A cond BTh_condflag_broadcast (&flag_AB,&cond_AB,&mutex); Pthread_mutex_unlock (&mutex); Usleep ( -); } //prevents the C thread from being deadlocked the last time a thread waitsFlag_ca =1;}void*th_fun_b (void*Arg) { inti =Lp_times; while(i--) {Pthread_mutex_lock (&mutex); //B Wait ATh_condflag_wait (&flag_AB,&cond_AB,&mutex); printf ("b%d:%lx\n",Ten-i,pthread_self ()); //B cond CTh_condflag_broadcast (&flag_BC,&cond_BC,&mutex); Pthread_mutex_unlock (&mutex); Usleep ( -); }}void*th_fun_c (void*Arg) { inti =Lp_times; while(i--) {Pthread_mutex_lock (&mutex); //C Wait BTh_condflag_wait (&flag_BC,&cond_BC,&mutex); printf ("c%d:%lx\n",Ten-i,pthread_self ()); //C cond ATh_condflag_broadcast (&flag_CA,&cond_CA,&mutex); Pthread_mutex_unlock (&mutex); Usleep ( -); }}intMainvoid) {printf ("Main:%lx\n", Pthread_self ()); //Save 3 threads of processing function void* (*th_funs[]) (void*) ={th_fun_a, Th_fun_b, Th_fun_c}; intTh_count =sizeof(th_funs)/sizeof(th_funs[0]); pthread_t Th[th_count]; inti; for(i =0; I < th_count;i++) { //create a thread by using a function that is logged by a thread function array if(Pthread_create (Th+i,null,th_funs[i], (void*) i) <0) {fprintf (stderr,"th_create:%s\n", Strerror (errno)); Exit (1); } printf ("th[%d]:%lx\n", I,th[i]); } //initiate notification to a thread to prevent a and C deadlocksPthread_mutex_lock (&mutex); Th_condflag_broadcast (&flag_CA,&cond_CA,&mutex); Pthread_mutex_unlock (&mutex); //Recycle thread for(i =0; I < th_count;i++) {pthread_join (th[i],null); printf ("I:%d finished!\n", i); } return 0;}</pthread></errno></string></stdlib></stdio>
The pen test of Thunder