生产者消费者-线程-链表-队列实现
链表实现
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_mutex_t lock;
pthread_cond_t cond;
struct node
{
int iValue;
struct node *next;
}*head = NULL;
void customerCleanup(void *arg)
{
printf("customerCleanup\n");
free(arg);
pthread_mutex_unlock(&lock);
}
void *customer(void *arg)
{
struct node *p = NULL;
pthread_cleanup_push(customerCleanup, NULL);
while(1)
{
pthread_mutex_lock(&lock);
if(head == NULL)
pthread_cond_wait(&cond, &lock);
p = head;
head = head->next;
printf("The node`s iValue is %d\n", p->iValue);
free(p);
p = NULL;
pthread_mutex_unlock(&lock);
}
pthread_exit(NULL);
pthread_cleanup_pop(0);
}
int main(void)
{
pthread_mutex_init(&lock, NULL);
pthread_cond_init(&cond, NULL);
pthread_t pthid;
pthread_create(&pthid, NULL, customer, NULL);
for(int i = 0; i != 10; ++i)
{
sleep(1);
pthread_mutex_lock(&lock);
struct node *p = (struct node *)malloc(sizeof(struct node *));
p->iValue = i * 2;
p->next = head;
head = p;
pthread_mutex_unlock(&lock);
pthread_cond_signal(&cond);
}
pthread_cancel(pthid);
pthread_join(pthid, NULL);
pthread_mutex_destroy(&lock);
pthread_cond_destroy(&cond);
return 0;
}
队列实现
main.c
#include <stdio.h>
#include <unistd.h>
#include "queue.h"
#define OVER (-1)
struct node queue;
void *producer(void *arg)
{
for(int i = 0; i != 50; ++i)
{
put(&queue, i);
printf("Put %d!\n", i);
}
put(&queue, OVER);
pthread_exit(NULL);
}
void *customer(void *arg)
{
int temp;
while(1)
{
temp = get(&queue);
if(temp == OVER) break;
printf("Get %d\n", temp);
}
pthread_exit(NULL);
}
int main(void)
{
init(&queue);
pthread_t pthid1, pthid2;
pthread_create(&pthid1, NULL, producer, NULL);
pthread_create(&pthid2, NULL, customer, NULL);
pthread_join(pthid1, NULL);
pthread_join(pthid2, NULL);
destroy(&queue);
return 0;
}
queue.h
#include<stdio.h>
#include<pthread.h>
#include<string.h>
#define BUF_SIZE 16
struct node
{
int buf[BUF_SIZE];
int writePos, readPos;
pthread_mutex_t lock;
pthread_cond_t condNotFull;
pthread_cond_t condNotEmpty;
};
void init(struct node *queue)
{
memset(queue->buf, 0, sizeof(queue->buf));
queue->writePos = 0;
queue->readPos = 0;
pthread_mutex_init(&queue->lock, NULL);
pthread_cond_init(&queue->condNotFull, NULL);
pthread_cond_init(&queue->condNotEmpty, NULL);
}
void put(struct node *queue, int value)
{
pthread_mutex_lock(&queue->lock);
while((queue->writePos+1)%BUF_SIZE == queue->readPos)
{
printf("队列满了\n");
pthread_cond_wait(&queue->condNotFull, &queue->lock);
}
queue->buf[queue->writePos] = value;
queue->writePos = (queue->writePos+1)%BUF_SIZE;
pthread_cond_signal(&queue->condNotEmpty);
pthread_mutex_unlock(&queue->lock);
}
int get(struct node *queue)
{
int temp;
pthread_mutex_lock(&queue->lock);
while(queue->writePos == queue->readPos)
{
printf("队列空了\n");
pthread_cond_wait(&queue->condNotEmpty, &queue->lock);
}
temp = queue->buf[queue->readPos];
queue->readPos = (queue->readPos+1)%BUF_SIZE;
pthread_cond_signal(&queue->condNotFull);
pthread_mutex_unlock(&queue->lock);
return temp;
}
void destroy(struct node *queue)
{
pthread_mutex_destroy(&queue->lock);
pthread_cond_destroy(&queue->condNotEmpty);
pthread_cond_destroy(&queue->condNotFull);
}