C++中使用条件变量写一个生产者-消费者的代码示例
时间: 2023-09-16 14:05:53 浏览: 119
### 回答1:
以下是使用条件变量实现生产者-消费者模型的示例代码,假设有一个缓冲区可以放置整数:
```c
#include <pthread.h>
#include <stdio.h>
#define BUFFER_SIZE 10
int buffer[BUFFER_SIZE]; // 缓冲区
int count = 0; // 缓冲区中整数的数量
pthread_mutex_t mutex; // 互斥锁
pthread_cond_t producer_cond, consumer_cond; // 条件变量
void *producer(void *arg) {
int i;
for (i = 0; i < 100; i++) {
pthread_mutex_lock(&mutex);
while (count == BUFFER_SIZE) {
// 缓冲区已满,等待消费者消费
pthread_cond_wait(&producer_cond, &mutex);
}
buffer[count] = i;
count++;
printf("Produced: %d\n", i);
pthread_cond_signal(&consumer_cond); // 唤醒一个消费者线程
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *consumer(void *arg) {
int i;
while (1) {
pthread_mutex_lock(&mutex);
while (count == 0) {
// 缓冲区已空,等待生产者生产
pthread_cond_wait(&consumer_cond, &mutex);
}
i = buffer[count - 1];
count--;
printf("Consumed: %d\n", i);
pthread_cond_signal(&producer_cond); // 唤醒一个生产者线程
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main() {
pthread_t producer_thread, consumer_thread;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&producer_cond, NULL);
pthread_cond_init(&consumer_cond, NULL);
pthread_create(&producer_thread, NULL, producer, NULL);
pthread_create(&consumer_thread, NULL, consumer, NULL);
pthread_join(producer_thread, NULL);
pthread_join(consumer_thread, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&producer_cond);
pthread_cond_destroy(&consumer_cond);
return 0;
}
```
上述代码中,生产者线程负责向缓冲区中生产整数,消费者线程负责从缓冲区中消费整数。生产者和消费者之间通过互斥锁和条件变量进行同步和通信。其中,生产者线程在缓冲区已满时等待消费者消费,消费者线程在缓冲区已空时等待生产者生产。当生产者向缓冲区中生产了一个整数时,唤醒一个消费者线程;当消费者从缓冲区中消费了一个整数时,唤醒一个生产者线程。这样就能够保证生产者和消费者之间的同步和通信。
### 回答2:
示例代码如下:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#define BUFFER_SIZE 10
int buffer[BUFFER_SIZE];
int count = 0;
int in = 0;
int out = 0;
pthread_mutex_t mutex;
pthread_cond_t cond_var_producer;
pthread_cond_t cond_var_consumer;
void *producer(void *arg) {
int i;
for (i = 0; i < 20; i++) {
pthread_mutex_lock(&mutex);
if (count == BUFFER_SIZE) {
pthread_cond_wait(&cond_var_producer, &mutex);
}
buffer[in] = i;
in = (in + 1) % BUFFER_SIZE;
count++;
printf("Producer produced item %d\n", i);
pthread_cond_signal(&cond_var_consumer);
pthread_mutex_unlock(&mutex);
usleep(100000);
}
pthread_exit(NULL);
}
void *consumer(void *arg) {
int i;
for (i = 0; i < 20; i++) {
pthread_mutex_lock(&mutex);
if (count == 0) {
pthread_cond_wait(&cond_var_consumer, &mutex);
}
int item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count--;
printf("Consumer consumed item %d\n", item);
pthread_cond_signal(&cond_var_producer);
pthread_mutex_unlock(&mutex);
usleep(100000);
}
pthread_exit(NULL);
}
int main() {
pthread_t producer_thread, consumer_thread;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond_var_producer, NULL);
pthread_cond_init(&cond_var_consumer, NULL);
pthread_create(&producer_thread, NULL, producer, NULL);
pthread_create(&consumer_thread, NULL, consumer, NULL);
pthread_join(producer_thread, NULL);
pthread_join(consumer_thread, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond_var_producer);
pthread_cond_destroy(&cond_var_consumer);
return 0;
}
以上代码使用了条件变量和互斥锁来实现生产者-消费者的模型。生产者在缓冲区满时会等待消费者消耗一个产品后才能继续生产,消费者在缓冲区空时会等待生产者生产一个产品后才能继续消费。
### 回答3:
生产者-消费者问题是一个经典的并发编程问题,可以通过条件变量来解决。条件变量是用于线程间通信的一种机制,它通过一个线程通知另外一个线程某个条件发生或者不发生,以便让另外一个线程做出相应的处理。
在C语言中,可以使用pthread库来实现条件变量。下面是一个生产者-消费者的代码示例:
```c
#include <stdio.h>
#include <pthread.h>
#define BUFFER_SIZE 10
int buffer[BUFFER_SIZE];
int count = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 互斥量
pthread_cond_t count_cond = PTHREAD_COND_INITIALIZER; // 条件变量
void* producer(void* arg) {
for (int i = 0; i < 20; i++) {
pthread_mutex_lock(&mutex);
// 如果缓冲区已满,则等待消费者消费产品
while (count == BUFFER_SIZE) {
pthread_cond_wait(&count_cond, &mutex);
}
buffer[count++] = i;
printf("Producer: produce item %d\n", i);
// 通知消费者有新产品可以消费
pthread_cond_signal(&count_cond);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
void* consumer(void* arg) {
for (int i = 0; i < 20; i++) {
pthread_mutex_lock(&mutex);
// 如果缓冲区为空,则等待生产者生产产品
while (count == 0) {
pthread_cond_wait(&count_cond, &mutex);
}
int item = buffer[--count];
printf("Consumer: consume item %d\n", item);
// 通知生产者有空位可以继续生产
pthread_cond_signal(&count_cond);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
int main() {
pthread_t producer_thread, consumer_thread;
pthread_create(&producer_thread, NULL, producer, NULL);
pthread_create(&consumer_thread, NULL, consumer, NULL);
pthread_join(producer_thread, NULL);
pthread_join(consumer_thread, NULL);
return 0;
}
```
上述代码中,生产者线程不断向缓冲区中生产物品,而消费者线程不断从缓冲区中消费物品。当缓冲区已满时,生产者线程会阻塞并等待消费者线程消费物品,而当缓冲区为空时,消费者线程会阻塞并等待生产者线程生产物品。生产者线程通过条件变量`count_cond`向消费者线程发送通知,消费者线程通过条件变量`count_cond`向生产者线程发送通知,从而实现线程间的同步与通信。
阅读全文