Skip to content
The Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

#include<pthread.h> #include<stdio.h> #include<stdlib.h> #include<unistd.h> #define OVER…

  1. User

    #include<pthread.h> #include<stdio.h> #include<stdlib.h> #include<unistd.h> #define OVER -1 #define SIZE 10 int ring_buffer[SIZE]; pthread_mutex_t mutex; //mutex lock pthread_cond_t not_full, not_empty; //avoid jingzheng
    int write_index =0; int read_index = 0;

    void *producer (void *arg){ int N = 0;// the item which will be written in buffer while(1){ pthread_mutex_lock(&mutex);//lock the thread first // test whether the buffer is full or not, if it's full wait until not full // the buffer is a ring_array while(write_index + 1 == read_index ||( read_index == 0 && write_index== SIZE -1 )){ // wait the thread until satisfy the condition variable pthread_cond_wait(& not_full, & mutex); } N = rand()%10000+1; if (N >100000){ ring_buffer[write_index] = OVER; printf("exit producer"); pthread_mutex_unlock(&mutex); break; } else { ring_buffer[write_index] = N; printf("%d has been written in buffer%d!\n",N,write_index); write_index = (write_index + 1)% SIZE;//update index if (write_index == read_index){ //add this condition to avoid awake more.. pthread_cond_signal(&not_empty);// awake the consumer thread } pthread_mutex_unlock(&mutex); sleep(1); } } pthread_exit(NULL); } void *consumer(void *arg){ while(1){ pthread_mutex_lock(&mutex);//lock the thread first // test whether the buffer is empty or not, if it's empty wait until not full // the buffer is a ring_array while(write_index == read_index){ // wait the thread until satisfy the condition variable pthread_cond_wait(& not_empty, & mutex); } int d = ring_buffer[read_index]; ring_buffer[read_index] = 0; //clear the item printf("an item %d in buffer %d has been read !\n",d,read_index); if (d == OVER){ printf("exit consumer!"); pthread_mutex_unlock(&mutex); break; } else{ read_index = (read_index + 1)% SIZE;//update index if (write_index + 1 == read_index ||( read_index == 0 && write_index== SIZE -1 )){ //add this condition to avoid awake more.. pthread_cond_signal(&not_full);// awake the consumer thread } pthread_mutex_unlock(&mutex); sleep(1); } } pthread_exit(NULL); }

    int main (){ pthread_mutex_init(&mutex,NULL); pthread_cond_init(&not_empty,NULL); pthread_cond_init(&not_full,NULL); srand(time(NULL));// int

    pthread_t t1,t2;
    int res1,res2;
    res1 = pthread_create(&t1,NULL,&producer,NULL);
    res2 = pthread_create(&t2,NULL,&consumer,NULL);
    
    if (res1 != 0) {
        printf("线程1创建失败");
        return 0;
    }
    else if(res2 != 0){
        printf("Thread2 failed to init");
        return 0;
    }
    pthread_join(t1,NULL);
    pthread_join(t2,NULL);
    

    pthread_mutex_destroy(&mutex); pthread_cond_destroy(&not_empty); pthread_cond_destroy(&not_full);

    }为什么我的消费者进程运行了一会儿就阻塞了

  2. Assistant

    你的代码中有一个问题,可能导致消费者线程阻塞。在生产者线程的else分支中,你应该先唤醒消费者线程,然后再解锁互斥锁。同样,消费者线程的else分支中,你应该先唤醒生产者线程,然后再解锁互斥锁。

    将生产者线程的else分支部分修改为:

    else {
        ring_buffer[write_index] = N;
        printf("%d has been written in buffer%d!\n",N,write_index);
        write_index = (write_index + 1)% SIZE;//update index
        pthread_cond_signal(&not_empty);// awake the consumer thread
        pthread_mutex_unlock(&mutex);
    }
    

    将消费者线程的else分支部分修改为:

    else{
        read_index = (read_index + 1)% SIZE;//update index
        pthread_cond_signal(&not_full);// awake the consumer thread
        pthread_mutex_unlock(&mutex);
    }
    

    这样修改后,生产者和消费者线程应该能正常运行,且互相唤醒。