多线程多个消费者与生产者(c++版)

#include<pthread.h>
#include<stdio.h>
/*
设计目的:通过研究Linux 的进程机制和信号量实现生产者消费者问题的并发控制.
说明:有界缓冲区内设有20个存储单元,放入/取出的数据项设定为1-20这20个整型数.
设计要求:
(1)每个生产者和消费者对有界缓冲区进行操作后,即时显示有界缓冲区的全部内容,当前指针位置和生产者/消费者线程的标识符.
(2)生产者和消费者各有两个以上.
(3)多个生产者或多个消费者之间须有共享对缓冲区进行操作的函数代码.
提示:(1) 有界缓冲区可用数组实现.


*/
pthread_mutex_t mu;
pthread_cond_t cond;
//pthread_cond_wait(&clifd_cond, &clifd_mutex);   pthread_cond_signal(&clifd_cond);
int g=0;
int i=0;
char gc[21];
void* create1(void *arg)//huoqu
{
    while(1){
        pthread_mutex_lock(&mu);
        if(g>=0&&g<20)
        {
int j=0;
for(i=1;i<=19;i++)
{
if(gc[i]=='1'&&gc[i+1]=='0')
{
printf("sc%d\n",i);
gc[i+1]='1';
j=1;break;
}
}
if(j==0){g=1;gc[1]='1';}else{g=i+1;}
            printf("%lu生产1:%s g is:%d\n",pthread_self(),gc,g);
pthread_cond_signal(&cond);
        }else{pthread_cond_wait(&cond, &mu);}
         pthread_mutex_unlock(&mu);
    }
}


void* create2(void *arg)//huoqu
{
     while(1){
        pthread_mutex_lock(&mu);
        if(g>=0&&g<20)
        {
int j=0;
for(i=1;i<=19;i++)
{
if(gc[i]=='1'&&gc[i+1]=='0')
{
printf("sc%d\n",i);
gc[i+1]='1';
j=1;break;
}
}
if(j==0){g=1;gc[1]='1';}else{g=i+1;}
            printf("%lu生产2:%s g is:%d\n",pthread_self(),gc,g);
pthread_cond_signal(&cond);
        }else{pthread_cond_wait(&cond, &mu);}
         pthread_mutex_unlock(&mu);
    }
}
void* create3(void *arg)//huoqu
{
   while(1){
        pthread_mutex_lock(&mu);
        if(g>=0&&g<20)
        {
int j=0;
for(i=1;i<=19;i++)
{
if(gc[i]=='1'&&gc[i+1]=='0')
{
printf("sc%d\n",i);
gc[i+1]='1';
j=1;break;
}
}
if(j==0){g=1;gc[1]='1';}else{g=i+1;}
            printf("%lu生产3:%s g is:%d\n",pthread_self(),gc,g);
pthread_cond_signal(&cond);
        }else{pthread_cond_wait(&cond, &mu);}
         pthread_mutex_unlock(&mu);
    }
}


void* create4(void *arg)//huoqu
{
    while(1){
        pthread_mutex_lock(&mu);
        if(g>=1)
        {
int j=0;
for(i=1;i<=19;i++)
{
 if(gc[i]=='1'&&gc[i+1]=='0'){j=1;gc[i]='0';break;}//
}
if(j!=1 && gc[20]!='1'){gc[1]='0';g = 0;}else if(j==0 && gc[20]=='1'){gc[20]='0';}else{g = i-1;}
            printf("%lu消费1:%s g is:%d\n",pthread_self(),gc,g);
pthread_cond_signal(&cond);
        }else{pthread_cond_wait(&cond, &mu);}
         pthread_mutex_unlock(&mu);
    }
}
void* create5(void *arg)//huoqu
{
     while(1){
        pthread_mutex_lock(&mu);
        if(g>=1)
        {
int j=0;
for(i=1;i<=19;i++)
{
 if(gc[i]=='1'&&gc[i+1]=='0'){j=1;gc[i]='0';break;}//
}
if(j!=1 && gc[20]!='1'){gc[1]='0';g = 0;}else if(j==0 && gc[20]=='1'){gc[20]='0';}else{g = i-1;}
            printf("%lu消费2:%s g is:%d\n",pthread_self(),gc,g);
pthread_cond_signal(&cond);
        }else{pthread_cond_wait(&cond, &mu);}
         pthread_mutex_unlock(&mu);
    }
}
void* create6(void *arg)//huoqu
{
     while(1){
        pthread_mutex_lock(&mu);
        if(g>=1)
        {
int j=0;
for(i=1;i<=19;i++)
{
 if(gc[i]=='1'&&gc[i+1]=='0'){j=1;gc[i]='0';break;}//
}
if(j!=1 && gc[20]!='1'){gc[1]='0';g = 0;}else if(j==0 && gc[20]=='1'){gc[20]='0';}else{g = i-1;}
            printf("%lu消费3:%s g is:%d\n",pthread_self(),gc,g);
pthread_cond_signal(&cond);
        }else{pthread_cond_wait(&cond, &mu);}
         pthread_mutex_unlock(&mu);
    }
}
int main(){
for(i=0;i<21;i++){gc[i]='0';}
    pthread_t produce1,produce2,produce3,consumer1,consumer2,consumer3;
  pthread_create(&consumer1,NULL,create4,NULL);
    pthread_create(&consumer2,NULL,create5,NULL);
    pthread_create(&consumer3,NULL,create6,NULL);
    pthread_create(&produce1,NULL,create1,NULL);
    pthread_create(&produce2,NULL,create2,NULL);
    pthread_create(&produce3,NULL,create3,NULL);
  pthread_cond_wait(&cond, &mu);
    pthread_join(consumer2,NULL);
    pthread_join(produce1,NULL);
    pthread_join(produce2,NULL);
    pthread_join(consumer1,NULL);
    pthread_join(produce3,NULL);
    pthread_join(consumer3,NULL);
return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在C语言中实现多线程生产者消费者模型可以使用线程库pthread。下面是一个简单的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define BUFFER_SIZE 5 int buffer[BUFFER_SIZE]; int count = 0; // 缓冲区中的数据数量 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 互斥锁 pthread_cond_t empty = PTHREAD_COND_INITIALIZER; // 缓冲区为空的条件变量 pthread_cond_t full = PTHREAD_COND_INITIALIZER; // 缓冲区为满的条件变量 void* producer(void* arg) { int item; while (1) { item = rand() % 100; // 生成随机数作为生产的物品 pthread_mutex_lock(&mutex); // 加锁 while (count == BUFFER_SIZE) { // 如果缓冲区已满,等待 pthread_cond_wait(&empty, &mutex); } buffer[count++] = item; // 将物品放入缓冲区 printf("Producer produced item: %d\n", item); pthread_cond_signal(&full); // 唤醒等待的消费者 pthread_mutex_unlock(&mutex); // 解锁 } return NULL; } void* consumer(void* arg) { int item; while (1) { pthread_mutex_lock(&mutex); // 加锁 while (count == 0) { // 如果缓冲区为空,等待 pthread_cond_wait(&full, &mutex); } item = buffer[--count]; // 从缓冲区取出物品 printf("Consumer consumed item: %d\n", item); pthread_cond_signal(&empty); // 唤醒等待的生产者 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; } ``` 这个示例代码中,定义了一个大小为5的缓冲区(使用数组实现),其中`count`变量表示缓冲区中的数据数量。生产者线程通过生成随机数作为物品,并将物品放入缓冲区消费者线程从缓冲区中取出物品并进行消费。互斥锁`mutex`用于保护临界区资源的访问,条件变量`empty`和`full`用于实现生产者消费者之间的同步。 请注意,这只是一个简单的示例代码,没有考虑线程安全性和错误处理。在实际使用中,还需要更加细致的设计和处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值