pthread_cond_signal(&empty);//这句不执行,下句pthread_mutex_unlock(&lock);也跟着不执行?
#include <stdio.h>#include <pthread.h>/*仓库*/char stack[6];int size=0;/*互斥量,用来保护临界资源*/pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER;/*条件变量,相当于线程的等待队列*/pthread_cond_t full=PTHREAD_COND_INITIALIZER;pthread_cond_t empty=PTHREAD_COND_INITIALIZER;/*打印仓库*/void print(){ int i; for(i=0; i<size; i++){ printf("%c ", stack[i]); } printf("\n");}/*生产者*/void* producer(void* p){ char c; for(c='A'; c<='Z'; c++){ pthread_mutex_lock(&lock); while(size==6){ pthread_cond_wait(&full, &lock); } stack[size] = c; printf("%c PUT\n", c); size++; print(); pthread_cond_signal(&empty); pthread_mutex_unlock(&lock); usleep(200*1000); }}/*消费者*/void* customer(void* p){ int i; for(i=0; i<52; i++){ pthread_mutex_lock(&lock); while(size==0) pthread_cond_wait(&empty, &lock);//&lock是什么意思? size--; printf("%c POP\n", stack[size]); print(); pthread_cond_broadcast(&full); pthread_mutex_unlock(&lock); usleep(400*1000); }}int main(){ pthread_t pid1, pid2, cid; pthread_create(&pid1, 0, producer, 0); pthread_create(&pid2, 0, producer, 0); pthread_create(&cid, 0, customer, 0); pthread_join(pid1, 0); pthread_join(pid2, 0); pthread_join(cid, 0);}#include <stdio.h>#include <stdlib.h>#include <pthread.h>static int num_products = 0;static int num_room = 10;static int num_to_make = 50;static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;static void* factory(void *arg) { int to_make = 0; for (; to_make != num_to_make; ++ to_make) { pthread_mutex_lock(&mutex); while (num_products == num_room) { pthread_cond_wait(&cond, &mutex); } ++ num_products; fprintf(stderr, "factory makes %dth product, storage %d\n", to_make, num_products); pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); } return NULL;}static void* customer(void *arg) { int to_use = 0; for (; to_use != num_to_make; ++ to_use) { pthread_mutex_lock(&mutex); while (num_products == 0) { pthread_cond_wait(&cond, &mutex); } -- num_products; fprintf(stderr, "customer gets %dth product, storage %d\n", to_use, num_products); pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); } return NULL;}int main(int argc, char* const argv[]) { pthread_t fac, cus; pthread_create(&fac, NULL, factory, NULL); pthread_create(&cus, NULL, customer, NULL); void *ret; pthread_join(fac, &ret); pthread_join(cus, &ret); return 0;}
[解决办法]
#include <stdio.h>#include <stdlib.h>#include <pthread.h>/* * 1:n or m:n * 两个条件变量的用法. * */static pthread_mutex_t full_mutex = PTHREAD_MUTEX_INITIALIZER;static pthread_cond_t full_cond = PTHREAD_COND_INITIALIZER;static pthread_mutex_t empty_mutex = PTHREAD_MUTEX_INITIALIZER;static pthread_cond_t empty_cond = PTHREAD_COND_INITIALIZER;static int num_space = 10;static int num_full = 0; //customer可取个数static int num_empty = 10; //factory可放个数static int num_to_make = 50;static void* factory(void *arg) { int ndx; for (ndx = 0; ndx != num_to_make; ++ ndx) { pthread_mutex_lock(&empty_mutex); while (num_empty == 0) { pthread_cond_wait(&empty_cond, &empty_mutex); } -- num_empty; pthread_mutex_unlock(&empty_mutex); pthread_mutex_lock(&full_mutex); ++ num_full; fprintf(stderr, "factory %dth storage %d\n", ndx, num_full); pthread_cond_signal(&full_cond); pthread_mutex_unlock(&full_mutex); } return NULL;}static void* customer(void *arg) { int ndx; for (ndx = 0; ndx != num_to_make; ++ ndx) { pthread_mutex_lock(&full_mutex); while (num_full == 0) { pthread_cond_wait(&full_cond, &full_mutex); } -- num_full; pthread_mutex_unlock(&full_mutex); pthread_mutex_lock(&empty_mutex); ++ num_empty; fprintf(stderr, "customer %dth storage %d\n", ndx, 10 - num_empty); pthread_cond_signal(&empty_cond); pthread_mutex_unlock(&empty_mutex); } return NULL;}int main(int argc, char* const argv[]) { pthread_t fac, cus; void *ret; /* 不模拟1:n, m:n了, 情景不适合 */ pthread_create(&fac, NULL, factory, NULL); pthread_create(&cus, NULL, customer, NULL); pthread_join(fac, &ret); pthread_join(cus, &ret); return 0;}