C语言线程实例(生产者和消费者)

附上代码,作为以后的参考。

/***********main.c************/

#include "customer.h"
#include "producer.h"
#include "data.h"
#include <pthread.h>
#include <stdio.h>

/*******************************************************************
**实现生产者线程生产数据放入一个单向链表中,消费者线程负责消费数据*****
*******************************************************************/

int main(){
   pthread_t thread_pro;
   pthread_t thread_cons;
   printf("create....\n");   

   //创建生产者线程。
   pthread_create(&thread_pro,NULL,(void *)producer,NULL);

   //创建消费者线程。
   pthread_create(&thread_cons,NULL,(void *)customer,NULL);
   printf("finished!\n");
   while(1){
   }
   return 0;
}

/************data.h***********/

#ifndef DATA_H_
#define DATA_H_

//单向链表数据结构
struct product{
  struct product *pre_product;
  char product_data[20];
  struct product *next_product;
};

//向单向链表中加入一个节点(生产)。
void addProduct(char *product_data);

//从单向链表中取出全部节点信息(消费)。
void consProduct();

#endif

/***********data.c************/

#include "data.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct product *present_product=NULL;

struct product *pre_product = NULL;

int lock=0;


void addProduct(char *product_data){
    while(lock==1);
    lock=1;
    struct product *new_product=malloc(sizeof(struct product));
    if(present_product==NULL){
      new_product->pre_product=NULL;
      strcpy( new_product->product_data,product_data);
      new_product->next_product=NULL;
      present_product=new_product;
    }else{
      new_product->pre_product=present_product;
      strcpy( new_product->product_data,product_data);
      new_product->next_product=NULL;
      present_product->next_product=new_product;
      present_product=new_product;
    }
    lock=0;
}


void consProduct(){
    while(lock==1);
    lock=1;
    while(present_product!=NULL){
	    pre_product=present_product->pre_product;
	    printf("%s\n",present_product->product_data);
	    if(pre_product!=NULL){
	      pre_product->next_product=0;
	    }
	    free(present_product);
	    present_product=pre_product;
    }
    lock=0;
}

/*************producer.h*************/

#ifndef PRODUCER_H_
#define PRODUCER_H_

//生产者执行生产任务
void producer();

#endif

/*************producer.c**************/

#include "producer.h"
#include "data.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int temp_i=0;
void producer(){
    char temp[20]={0};
    while(1){
      sprintf(temp,"number___%d",temp_i);
      addProduct(temp);
      temp_i++;
      usleep((int)(rand()/2000));
    }
}

/************customer.h***********/

#ifndef CUSTOMER_H_
#define CUSTOMER_H_

//消费者执行消费任务。
void customer();

#endif

/*************customer.c*************/

#include "customer.h"
#include "data.h"
#include <stdlib.h>
#include <stdio.h>

void customer(){
  while(1){
    consProduct();
    printf("-------------\n");
    usleep((int)(rand()/1000));
  }
}

运行截图:

(---------完---------)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值