#include<pthread.h> #include<stdio.h> #include<stdlib.h> #define OVER -1 #define SIZE…
- User
#include<pthread.h> #include<stdio.h> #include<stdlib.h> #define OVER -1 #define SIZE 100 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(); if (N >1000){ ring_buffer[write_index] = OVER; } else ring_buffer[write_index] = N; printf("%d has been written in buffer%d!",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(¬_empty);// awake the consumer thread } pthread_mutex_unlock(&mutex); sleep(1); } } 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 item = ring_buffer[read_index]; ring_buffer[read_index] = 0; //clear the item printf("an item %d in buffer %d has been read !",item,read_index); 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(¬_full);// awake the consumer thread } pthread_mutex_unlock(&mutex); sleep(1); }
}
int main (){ pthread_mutex_init(&mutex,NULL); pthread_cond_init(¬_empty,NULL); pthread_cond_init(¬_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(¬_empty); pthread_cond_destroy(¬_full);
}哪里有问题?
- Assistant
您的程序在大部分情况下应该可以正常运行,但是有一个小问题可能会导致不符合预期的行为:
-
当生产者线程生成的随机数 N 大于 1000 时,您将 -1 写入 ring_buffer,但是消费者线程中没有检查这种情况。您可以在消费者线程中添加以下条件来处理这种情况:
修改 consumer 函数中的代码:
void *consumer(void *arg){ while(1){ pthread_mutex_lock(&mutex); while(write_index == read_index){ pthread_cond_wait(& not_empty, & mutex); } int item = ring_buffer[read_index]; if (item == OVER) { printf("Producer is done."); pthread_mutex_unlock(&mutex); break; // 退出循环,结束消费者线程 } ring_buffer[read_index] = 0; printf("an item %d in buffer %d has been read !",item,read_index); read_index = (read_index + 1)% SIZE; if (write_index + 1 == read_index ||( read_index == 0 && write_index== SIZE -1 )){ pthread_cond_signal(¬_full); } pthread_mutex_unlock(&mutex); sleep(1); } }这段代码会检查从 ring_buffer 中读取的值是否为 -1(OVER),如果是,则说明生产者已经完成生产,消费者线程可以退出循环并结束。
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.