生产者生产临界公共区变脸,消费者消费临界公共区变量
通过互斥锁和条件变量实现对临界公共区的控制。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
struct msg{
int num;
struct msg *next;
};
struct msg *head = NULL;
struct msg *mp = NULL;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t has_product = PTHREAD_COND_INITIALIZER;
void *producter(void *arg)
{
while(1){
mp = malloc(sizeof(struct msg));
mp->num = rand()%400+1;
printf("---producted---%d\n",mp->num);
pthread_mutex_lock(&mutex);
mp->next = head;
head = mp;
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&has_product);
sleep(rand()%3);
}
return NULL;
}
void *consumer(void *arg)
{
while(1){
pthread_mutex_lock(&mutex);
while(head==NULL){
pthread_cond_wait(&has_product,&mutex);
}
mp = head;
head = mp->next;
pthread_mutex_unlock(&mutex);
printf("------consumer----%d\n",mp->num);
free(mp);
mp = NULL;
}
}
int main()
{
pthread_t ptid, ctid;
srand(time(NULL));
pthread_create(&ptid,NULL, producter,NULL);
pthread_create(&ctid,NULL, consumer,NULL);
pthread_join(ptid, NULL);
pthread_join(ctid, NULL);
}