用互斥量解决生产中-消费者问题
当生产者的生产速度低于消费者的消费速度时,消费者必须等待,所以就会产生多次不必的锁资源申请释放动作。影响系统性能。
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
typedef struct _Node
{
int val;
struct _Node* next;
}Node;
typedef struct _Args
{
Node* head;
}Args;
Node* head;
void Init()
{
head=NULL;
}
void PushBack(int val)
{
Node* node=(Node*)malloc(sizeof(Node));
node->val=val;
node->next=NULL;
if(head==NULL)
{
head=node;
return;
}
Node* temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=node;
}
int PopFront()
{
if(head==NULL)
{
printf("list is empty\n");
return -1;
}
Node* temp=head;
head=(head)->next;
int res=temp->val;
free(temp);
return res;
}
void PrintList()
{
Node* temp=head;
while(temp!=NULL)
{
printf("%4d",temp->val);
temp=temp->next;
}
printf("\n");
}
void* Consumer(void* args)
{
for(int i=0;i<10;i++)
{
pthread_mutex_lock(&mutex);
int res=PopFront();
if(res!=-1)
{
printf("In consumer:[%d]\n",res);
}
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
void* Producter(void* args)
{
for(int i=0;i<10;i++)
{
pthread_mutex_lock(&mutex);
PushBack(i);
printf("In Producter:[%d]\n",i);
pthread_mutex_unlock(&mutex);
sleep(2);
}
}
int main()
{
Init(&head);
Args args;
args.head=head;
pthread_t pid[2];
pthread_create(&pid[0],NULL,Producter,(void*)&args);
pthread_create(&pid[1],NULL,Consumer,(void*)&args);
pthread_join(pid[0],NULL);
pthread_join(pid[1],NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
结果如下
添加条件变量之后
把模型改为最多只能保留5个东西,
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condp=PTHREAD_COND_INITIALIZER;
pthread_cond_t condc=PTHREAD_COND_INITIALIZER;
#define MAXLEN 5
typedef struct _Node
{
int val;
struct _Node* next;
}Node;
typedef struct _Args
{
Node* head;
}Args;
Node* head;
int size;
void Init()
{
head=NULL;
size=0;
}
void PushBack(int val)
{
Node* node=(Node*)malloc(sizeof(Node));
node->val=val;
node->next=NULL;
if(head==NULL)
{
head=node;
return;
}
Node* temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=node;
size++;
}
int PopFront()
{
if(head==NULL)
{
printf("list is empty\n");
return -1;
}
Node* temp=head;
head=(head)->next;
int res=temp->val;
free(temp);
size--;
return res;
}
void PrintList()
{
Node* temp=head;
while(temp!=NULL)
{
printf("%4d",temp->val);
temp=temp->next;
}
printf("\n");
}
void* Consumer(void* args)
{
while(1)
{
pthread_mutex_lock(&mutex);
while(size==0)
{
pthread_cond_wait(&condc,&mutex);
}
int res= PopFront();
if(size<MAXLEN)
{
pthread_cond_signal(&condp);
}
printf("In consumer:[%d]\n",res);
pthread_mutex_unlock(&mutex);
sleep(3);
}
}
void* Producter(void* args)
{
while(1)
{
pthread_mutex_lock(&mutex);
while(size==MAXLEN)
{
pthread_cond_wait(&condp,&mutex);
}
int i=rand()%1000;
PushBack(i);
printf("size = %d\n",size);
if(size>0)
{
pthread_cond_signal(&condc);
}
printf("In Producter:[%d]\n",i);
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
int main()
{
Init(&head);
Args args;
args.head=head;
pthread_t pid[2];
pthread_create(&pid[1],NULL,Consumer,(void*)&args);
pthread_create(&pid[0],NULL,Producter,(void*)&args);
pthread_join(pid[0],NULL);
pthread_join(pid[1],NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&condp);
pthread_cond_destroy(&condc);
return 0;
}
结果如图