日期:2014-05-16  浏览次数:20745 次

pthread_cond_signal(&empty);//这句不执行,下句pthread_mutex_unlock(&lock);也跟着不执行?
C/C++ code

#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);
}




------解决方案--------------------
C/C++ code
#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;
}

------解决方案--------------------
C/C++ code
#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* fact