生产者消费者-线程-链表-队列实现

生产者消费者-线程-链表-队列实现


链表实现

#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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值