The picture is reproduced, the source is not clear.
#include <stdio.h>#include<stdlib.h>#include<string.h>#include<pthread.h>#include<errno.h>pthread_cond_t cond_a;pthread_cond_t cond_b;pthread_cond_t cond_c;pthread_mutex_t lock_a; pthread_mutex_t Lock_b ; pthread_mutex_t Lock_c; void* Funa (void*Arg) { inti; for(i =0; i<Ten; i++) { if(Pthread_mutex_lock (&lock_a)) {printf ("Lock failure\n"); Exit (0); } printf ("a[%d]\n", i); if(Pthread_cond_signal (&cond_b)) {printf ("Lock failure\n"); Exit (0); } if(Pthread_cond_wait (&cond_a, &lock_a)) {printf ("Lock failure\n"); Exit (0); } if(Pthread_mutex_unlock (&lock_a)) {printf ("Lock failure\n"); Exit (0); }} printf ("A exit\n"); Pthread_exit (NULL);}void* FUNB (void*Arg) { inti; for(i =0; i<Ten; i++) { if(Pthread_mutex_lock (&Lock_b)) {printf ("Lock failure\n"); Exit (0); } printf ("b[%d]", i); if(Pthread_cond_signal (&Cond_c)) {printf ("Lock failure\n"); Exit (0); } if(Pthread_cond_wait (&cond_b, &Lock_b)) {printf ("Lock failure\n"); Exit (0); } if(Pthread_mutex_unlock (&Lock_b)) {printf ("Lock failure\n"); Exit (0); }} printf ("A exit\n"); Pthread_exit (NULL);}void* Func (void*Arg) { inti; for(i =0; i<Ten; i++) { if(Pthread_mutex_lock (&Lock_c)) {printf ("Lock failure\n"); Exit (0); } printf ("c[%d]", i); if(Pthread_cond_signal (&cond_a)) {printf ("Lock failure\n"); Exit (0); } if(Pthread_cond_wait (&cond_c, &Lock_c)) {printf ("Lock failure\n"); Exit (0); } if(Pthread_mutex_unlock (&Lock_c)) {printf ("Lock failure\n"); Exit (0); }} printf ("A exit\n"); Pthread_exit (NULL);}intMainintargcChar*argv[]) {pthread_t TA,TB,TC; intret; inti; Pthread_mutex_init (&lock_a, NULL); Pthread_mutex_init (&Lock_b, NULL); Pthread_mutex_init (&Lock_c, NULL); Pthread_cond_init (&cond_a, NULL); Pthread_cond_init (&cond_b, NULL); Pthread_cond_init (&Cond_c, NULL); RET= Pthread_create (&ta, NULL, Funa, (void*)1); if(ret) {printf ("pthread_create ta error [%s]", Strerror (errno)); return 1; } ret= Pthread_create (&TB, NULL, Funb, (void*)2); if(ret) {printf ("pthread_create TB Error [%s]", Strerror (errno)); return 1; } ret= Pthread_create (&TC, NULL, Func, (void*)3); if(ret) {printf ("pthread_create TC Error [%s]", Strerror (errno)); return 1; } pthread_join (Ta, NULL); Pthread_join (TB, NULL); Pthread_join (TC, NULL); Pthread_cond_destroy (&cond_a); Pthread_cond_destroy (&cond_b); Pthread_cond_destroy (&Cond_c); Pthread_mutex_destroy (&lock_a); Pthread_mutex_destroy (&Lock_b); Pthread_mutex_destroy (&Lock_c); printf ("\nmain thread exit\n"); return 0;}
View Code
Why not run it. Save the next
----------------------------------------------------
Debugging for half a day, not not running, but there is a cache, and after the 10th time, a will exit, and B and C will wait, has been in a waiting state. It seems that before their doubts are no problem, the problem is not to consider the end of the state. Ready to DIY a printf function, this function is too pit. The state of the cache was not previously considered, but only when printing a was added \ n. Halo, considering this, my first version of the program is not known, but a does add a newline character. But the results didn't come out. After the change, the line is not added. This is a lot of pits.
Summing up, the understanding of the conditions is right, the mutual exclusion of the understanding is right, the end of the condition is not considered good. Failed.
I thought the thread didn't run and didn't run.