本文共 522 字,大约阅读时间需要 1 分钟。
#include#include #include #include #include #include using namespace std;const int ConsumerNum = 3;const int PruducterNum = 2;const int M = 20;//缓存区大小int in = 0;int out = 0;int buff[M] = {0};sem_t empty_sem;//表示空位的数量sem_t full_sem;//产品的数量pthread_mutex_t mutex;//线程在读取或产生时,都应该加锁int i=0;//产品Idvoid *product(void *){ while (true) { //sleep(1); sem_wait(&empty_sem);//空位数减1,已经把这个位置站住了,其他线程只能访问剩余位置 pthread_mutex_lock(&mutex); cout<<"producter "< <<" produce "< <
转载地址:http://useti.baihongyu.com/