phread 消息队列测试demo

#include 
#include 

#define MAX_QUEUE_SIZE 10

typedef struct QueueP_NodeData {
    int data;
} QueueP_NodeData_t;

typedef struct {
    QueueP_NodeData_t* buffer;
    int maxSize;
    int front;
    int rear;
    int itemCount;
    pthread_mutex_t lock;
    pthread_cond_t notFull;
    pthread_cond_t notEmpty;
} QueueP_t;

void QueueP_create(QueueP_t* queue, int maxSize) {
    queue->buffer = (QueueP_NodeData_t*)malloc(sizeof(QueueP_NodeData_t) * maxSize);
    queue->maxSize = maxSize;
    queue->front = 0;
    queue->rear = -1;
    queue->itemCount = 0;
    pthread_mutex_init(&queue->lock, NULL);
    pthread_cond_init(&queue->notFull, NULL);
    pthread_cond_init(&queue->notEmpty, NULL);
}

void QueueP_delete(QueueP_t* queue) {
    free(queue->buffer);
    pthread_mutex_destroy(&queue->lock);
    pthread_cond_destroy(&queue->notFull);
    pthread_cond_destroy(&queue->notEmpty);
}

void QueueP_get(QueueP_t* queue, QueueP_NodeData_t* item) {
    pthread_mutex_lock(&queue->lock);
    while (queue->itemCount == 0) {
        pthread_cond_wait(&queue->notEmpty, &queue->lock);
    }
    *item = queue->buffer[queue->front];
    queue->front = (queue->front + 1) % queue->maxSize;
    queue->itemCount--;
    pthread_cond_signal(&queue->notFull);
    pthread_mutex_unlock(&queue->lock);
}

void QueueP_put(QueueP_t* queue, QueueP_NodeData_t item) {
    pthread_mutex_lock(&queue->lock);
    while (queue->itemCount == queue->maxSize) {
        pthread_cond_wait(&queue->notFull, &queue->lock);
    }
    queue->rear = (queue->rear + 1) % queue->maxSize;
    queue->buffer[queue->rear] = item;
    queue->itemCount++;
    pthread_cond_signal(&queue->notEmpty);
    pthread_mutex_unlock(&queue->lock);
}

int QueueP_isEmpty(QueueP_t* queue) {
    return queue->itemCount == 0;
}

void* producer(void* arg) {
    QueueP_t* queue = (QueueP_t*)arg;
    for (int i = 0; i < 20; i++) {
        QueueP_NodeData_t data;
        data.data = i;
        printf("Producer: Enqueueing item %d\n", data.data);
        QueueP_put(queue, data);
        usleep(100000);
    }
    return NULL;
}

void* consumer(void* arg) {
    QueueP_t* queue = (QueueP_t*)arg;
    while (1) {
        QueueP_NodeData_t data;
        QueueP_get(queue, &data);
        printf("Consumer: Dequeueing item %d\n", data.data);
        usleep(500000);
    }
    return NULL;
}

int main() {
    QueueP_t queue;
    QueueP_create(&queue, MAX_QUEUE_SIZE);
    
    pthread_t producer_thread, consumer_thread;
    pthread_create(&producer_thread, NULL, producer, (void*)&queue);
    pthread_create(&consumer_thread, NULL, consumer, (void*)&queue);
    
    pthread_join(producer_thread, NULL);
    pthread_join(consumer_thread, NULL);
    
    QueueP_delete(&queue);
    
    return 0;
}`在这里插入代码片`

这段代码创建了一个基于pthread实现的队列数据结构,在初始化时需要指定队列的最大长度MAX_QUEUE_SIZE,并通过QueueP_create函数进行初始化。队列中保存的每个元素是QueueP_NodeData_t类型的结构体。QueueP_get和QueueP_put函数分别用于获取队列中的元素和向队列中放入元素。QueueP_isEmpty函数用于判断队列是否为空。

在main函数中,我们创建了一个生产者线程和一个消费者线程,它们并发地对队列进行操作。生产者线程循环向队列中插入数据,而消费者线程从队列中读取数据。通过使用条件变量和互斥锁,当队列为空时,消费者线程会

你可能感兴趣的:(数据结构与算法,c++)