c++版
#include <iostream>
#include <list>
#include <pthread.h>
#define MAX_SIZE 32
using namespace std;
class Storage {
private:
list<int> list;
pthread_mutex_t mutex;
pthread_cond_t cond;
void produce() {
/hread_mutex_lock(&mutex);
while (list.size() + 1 > MAX_SIZE) {
cout << "【生产者" << pthread_self() << "】仓库已满" << endl;
/hread_cond_wait(&cond, &mutex);
}
list.push_back(1);
cout << "【生产者" << pthread_self() << "】生产一个产品," << "生产产品位置为:" << list.size()
<< ",现库存" << list.size() << endl;
/hread_cond_signal(&cond);
/hread_mutex_unlock(&mutex);
}
void consume() {
/hread_mutex_lock(&mutex);
while