代码中有详细注释,详见readme.txt
这个主要是基于条件变量和互斥量来实现,如果缓存队列在没有满或者没有空的场景下,读写线程只需要在数据插入删除的时候持有锁,使用完释放锁就可以保证原子性。
当进入读写线程发现队列满或者空的时候,就需要使用条件变量把线程阻塞,并释放锁,当写线程插入数据后,就会发一个不空的信号去唤醒读线程。反之同理。
相关API
线程
pthread_create 传入线程id创建线程
pthread_join 主线程阻塞等待指定线程结束
初始化
pthread_mutex_init 初始化锁
pthread_cond_init 初始化条件变量
pthread_cond_wait 前要先加锁
pthread_cond_wait 内部会解锁,然后等待条件变量被其它线程激活
pthread_cond_wait 被激活后会再自动加锁
pthread_mutex_lock 加锁(读写线程用同一个锁)
pthread_mutex_unlock 解锁
pthread_cond_signal 发送信号
cmakelist
cmake_minimum_required(VERSION 3.17)
project(A)
set(CMAKE_CXX_STANDARD 14)
add_executable(A fun.cpp final.cpp)
#add_executable(A queue.c)
find_package(Threads REQUIRED)
target_link_libraries(A Threads::Threads)
fun.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "final.h"
#include <unistd.h>
#include<pthread.h>
struct Queue *init_queue()
{
struct Queue *queue=(struct Queue *)malloc(sizeof(struct Queue));
if(!queue)
{
exit(1);
}
queue->front=(struct Node*)malloc(sizeof(struct Node));
queue->front->next=NULL;
queue->rear=queue->front;
queue->len=0;
queue->max_len=MAX_LEN;
pthread_mutex_init(&queue->q_lock,NULL);
pthread_cond_init(&queue->not_empty,NULL);
pthread_cond_init(&queue->not_full,NULL);
return queue;
}
void en_queue(struct Queue *q,void* data,size_t len)//向队列的尾部插入一个数据,请传入数据类型的长度
{
pthread_mutex_lock(&q->q_lock);
while(q->len==MAX_LEN)
{
// printf("this queue is full\n");//这句使用是可以注释掉,会等待读函数删掉后再继续写
pthread_cond_wait(&q->not_full,&q->q_lock);
}
struct Node *newnode=(struct Node*)malloc(sizeof(struct Node));
newnode->data=malloc(len);
memcpy(newnode->data,data