user-0 提问: pthread条件变量问题

回复:
user: user-1
content: 2楼给出了问题的原因了。

user: user-2
content: 引用 3 楼 rejoy2009 的回复:[quote=引用 1 楼 q3733353520 的回复:]  #include <stdio.h> #include <pthread.h>  pthread_mutex_t count_lock; pthread_cond_t count_nonzero; unsigned count = 1;  #define MAX_COUNT 100  void mutex_clean(void *mutex) { 	pthread_mutex_unlock((pthread_mutex_t *) mutex); }  void *decrement_count(void *arg) { 	while (count < MAX_COUNT) 	{ 		pthread_mutex_lock(&count_lock); 		printf("decrement acq lock\n"); 		while(count % 2 != 0) 		{ 			printf("%d\n", count);  //放进来  			pthread_cond_wait(&count_nonzero, &count_lock); 		} 		pthread_mutex_unlock(&count_lock); 	//	Sleep(1); 	} 	return NULL; }  void *increment_count(void *arg) {     int i;     for (i = 0; i < MAX_COUNT; i++)     { 		pthread_mutex_lock(&count_lock); 		count++; 		pthread_cond_signal(&count_nonzero); 		pthread_mutex_unlock(&count_lock); 	  //	Sleep(1);     }     return NULL; }  int main(void) { 	pthread_t tid1, tid2;  	pthread_mutex_init(&count_lock, NULL); 	pthread_cond_init(&count_nonzero, NULL);  	pthread_create(&tid1, NULL, decrement_count, NULL); 	//Sleep(2); 	pthread_create(&tid2, NULL, increment_count, NULL);  	pthread_join(tid1, NULL); 	pthread_join(tid2, NULL);  	return 0; }   发现一般死循环是打印101,所以，是加到了101,然后唤醒 decrement_count，而 101%2  为1 又使 decrement_count 挂起，但是  increment_count 进不去for 那么线程结束，但是 decrement_count 一直在等待唤醒而没有结束那为啥加了Sleep就可以了呢？[/quote]  可能加到 100的时候 decrement_count在sleep而increment_count加到了101（时间很短）, 唤醒decrement_count时还在sleep，那么 decrement_count在sleep完后直接结束线程

user: user-0
content: 引用 1 楼 q3733353520 的回复: #include <stdio.h> #include <pthread.h>  pthread_mutex_t count_lock; pthread_cond_t count_nonzero; unsigned count = 1;  #define MAX_COUNT 100  void mutex_clean(void *mutex) { 	pthread_mutex_unlock((pthread_mutex_t *) mutex); }  void *decrement_count(void *arg) { 	while (count < MAX_COUNT) 	{ 		pthread_mutex_lock(&count_lock); 		printf("decrement acq lock\n"); 		while(count % 2 != 0) 		{ 			printf("%d\n", count);  //放进来  			pthread_cond_wait(&count_nonzero, &count_lock); 		} 		pthread_mutex_unlock(&count_lock); 	//	Sleep(1); 	} 	return NULL; }  void *increment_count(void *arg) {     int i;     for (i = 0; i < MAX_COUNT; i++)     { 		pthread_mutex_lock(&count_lock); 		count++; 		pthread_cond_signal(&count_nonzero); 		pthread_mutex_unlock(&count_lock); 	  //	Sleep(1);     }     return NULL; }  int main(void) { 	pthread_t tid1, tid2;  	pthread_mutex_init(&count_lock, NULL); 	pthread_cond_init(&count_nonzero, NULL);  	pthread_create(&tid1, NULL, decrement_count, NULL); 	//Sleep(2); 	pthread_create(&tid2, NULL, increment_count, NULL);  	pthread_join(tid1, NULL); 	pthread_join(tid2, NULL);  	return 0; }   发现一般死循环是打印101,所以，是加到了101,然后唤醒 decrement_count，而 101%2  为1 又使 decrement_count 挂起，但是  increment_count 进不去for 那么线程结束，但是 decrement_count 一直在等待唤醒而没有结束那为啥加了Sleep就可以了呢？

user: user-3
content: 如果A线程还没执行到wait处，B线程就发送了信号，那么A执行到wait时依然会阻塞即得不到唤醒。

user: user-2
content: #include <stdio.h> #include <pthread.h>  pthread_mutex_t count_lock; pthread_cond_t count_nonzero; unsigned count = 1;  #define MAX_COUNT 100  void mutex_clean(void *mutex) { 	pthread_mutex_unlock((pthread_mutex_t *) mutex); }  void *decrement_count(void *arg) { 	while (count < MAX_COUNT) 	{ 		pthread_mutex_lock(&count_lock); 		printf("decrement acq lock\n"); 		while(count % 2 != 0) 		{ 			printf("%d\n", count);  //放进来  			pthread_cond_wait(&count_nonzero, &count_lock); 		} 		pthread_mutex_unlock(&count_lock); 	//	Sleep(1); 	} 	return NULL; }  void *increment_count(void *arg) {     int i;     for (i = 0; i < MAX_COUNT; i++)     { 		pthread_mutex_lock(&count_lock); 		count++; 		pthread_cond_signal(&count_nonzero); 		pthread_mutex_unlock(&count_lock); 	  //	Sleep(1);     }     return NULL; }  int main(void) { 	pthread_t tid1, tid2;  	pthread_mutex_init(&count_lock, NULL); 	pthread_cond_init(&count_nonzero, NULL);  	pthread_create(&tid1, NULL, decrement_count, NULL); 	//Sleep(2); 	pthread_create(&tid2, NULL, increment_count, NULL);  	pthread_join(tid1, NULL); 	pthread_join(tid2, NULL);  	return 0; }   发现一般死循环是打印101,所以，是加到了101,然后唤醒 decrement_count，而 101%2  为1 又使 decrement_count 挂起，但是  increment_count 进不去for 那么线程结束，但是 decrement_count 一直在等待唤醒而没有结束