c语言多线程队列实现

为了用c语言实现队列进行多线程通信,用于实现一个状态机。
下面是实现过程

1.实现多线程队列入栈和出栈,不加锁

发送线程发送字符1,接收线程接收字符并打印。
多线程没有加锁,会有危险

#include "stdio.h"
#include <thread>
#include <unistd.h>
#include <pthread.h>typedef struct MutiThreadCharQueNode
{unsigned char data;struct MutiThreadCharQueNode* next;
}MutiThreadCharQueNode;typedef struct MutiThreadCharQueue
{MutiThreadCharQueNode* phead;MutiThreadCharQueNode* ptail;int size;
}MutiThreadCharQueue;
MutiThreadCharQueue TestMutiThreadQue;void MutiThreadCharQueueInit(MutiThreadCharQueue* pq)
{pq->phead=NULL; //将队列的头指针置为空pq->ptail = NULL;//将队列的尾指针置为空pq->size = 0;// 将队列的头指针置为空
}
bool MutiThreadCharQueueEmpty(MutiThreadCharQueue* pq)
{return pq->size == 0;
}
void MutiThreadCharQueueDestroy(MutiThreadCharQueue* pq)
{MutiThreadCharQueNode* cur = pq->phead;// 创建一个指针 cur,指向队列的头指针while (cur){MutiThreadCharQueNode* next = cur->next;// 创建一个指针 cur,指向队列的头指针free(cur);// 释放当前节点的内存cur = next;// 将指针 cur 移动到下一个节点}pq->phead = pq->ptail = NULL;// 将队列的头指针和尾指针置为空pq->size = 0;// 将队列的大小置为0
}
void MutiThreadCharQueuePush(MutiThreadCharQueue* pq, unsigned char x)
{MutiThreadCharQueNode* newnode = (MutiThreadCharQueNode*)malloc(sizeof(MutiThreadCharQueNode));// 创建一个新的节点if (newnode == NULL){return;}newnode->data = x;// 设置新节点的数据为传入的元素值newnode->next = NULL;// 将新节点的指针域置空//一个节点if (pq->ptail == NULL)// 判断队列是否为空{pq->phead = pq->ptail = newnode;// 将新节点同时设置为队列的头节点和尾节点}//多个节点else{pq->ptail->next = newnode;// 将新节点同时设置为队列的头节点和尾节点pq->ptail = newnode;// 更新队列的尾指针为新节点}pq->size++;// 增加队列的大小计数
}
unsigned char MutiThreadCharQueueFront(MutiThreadCharQueue* pq)
{
//	assert(pq);// 检查指针是否为空
//	assert(!QueueEmpty(pq));// 检查队列是否非空
//	assert(pq->phead);// 检查队列的头指针是否存在
//    if(QueueEmpty(pq))
//    {
//        return ;
//    }return pq->phead->data;// 返回队列头节点的数据
}
void MutiThreadCharQueuePop(MutiThreadCharQueue* pq)
{//1.一个节点if (pq->phead->next == NULL) // 队列只有一个节点的情况{free(pq->phead); // 释放队列头节点的内存pq->phead = pq->ptail = NULL;// 将队列的头指针和尾指针置为空}//2.多个节点else{MutiThreadCharQueNode* next = pq->phead->next; //保存队列头节点的下一个节点指针free(pq->phead);// 释放队列头节点的内存pq->phead = next;// 更新队列的头指针为下一个节点}pq->size--;//减少队列的大小计数
}
void* thread_send(void* para)
{printf("hh\n");MutiThreadCharQueueInit(&TestMutiThreadQue);unsigned char sendChar=1;while(1){printf("send a\n");MutiThreadCharQueuePush(&TestMutiThreadQue,sendChar);usleep(1000000);}
}
void* thread_rev(void* para)
{printf("h2\n");unsigned char revChar;while(1){if(false==MutiThreadCharQueueEmpty(&TestMutiThreadQue)){revChar=MutiThreadCharQueueFront(&TestMutiThreadQue);printf("rev char= %d\n",(int)revChar);MutiThreadCharQueuePop(&TestMutiThreadQue);}usleep(1000000);}
}void create_c_thread_send()
{int ret;pthread_attr_t attr;ret = pthread_attr_init(&attr);if (ret != 0) {return ;}//2pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);int err;pthread_t tid;err = pthread_create(&tid, &attr, thread_send, (void*)NULL);}
void create_c_thread_rev()
{int ret;pthread_attr_t attr;ret = pthread_attr_init(&attr);if (ret != 0) {return ;}//2pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);int err;pthread_t tid;err = pthread_create(&tid, &attr, thread_rev, (void*)NULL);}
int main(int argc, char** argv)
{printf("hello\n");create_c_thread_send();create_c_thread_rev();while(1){usleep(10000);}return 0;
}

2.实现多线程队列入栈和出栈,加锁并使用信号量触发接收线程

在队列的结构体中加上锁,防止多线程冲突

#include "stdio.h"
#include <thread>
#include <unistd.h>
#include <pthread.h>typedef struct MutiThreadCharQueNode
{unsigned char data;struct MutiThreadCharQueNode* next;
}MutiThreadCharQueNode;typedef struct MutiThreadCharQueue
{MutiThreadCharQueNode* phead;MutiThreadCharQueNode* ptail;int size;pthread_mutex_t mutex;
}MutiThreadCharQueue;
MutiThreadCharQueue TestMutiThreadQue;void MutiThreadCharQueueInit(MutiThreadCharQueue* pq)
{pq->phead=NULL; //将队列的头指针置为空pq->ptail = NULL;//将队列的尾指针置为空pq->size = 0;// 将队列的头指针置为空pthread_mutex_init(&pq->mutex, NULL);
}
bool MutiThreadCharQueueEmpty(MutiThreadCharQueue* pq)
{pthread_mutex_lock(&pq->mutex);bool bEmpty=(bool) (pq->size == 0);pthread_mutex_unlock(&pq->mutex);return bEmpty;
}
void MutiThreadCharQueueDestroy(MutiThreadCharQueue* pq)
{pthread_mutex_lock(&pq->mutex);MutiThreadCharQueNode* cur = pq->phead;// 创建一个指针 cur,指向队列的头指针while (cur){MutiThreadCharQueNode* next = cur->next;// 创建一个指针 cur,指向队列的头指针free(cur);// 释放当前节点的内存cur = next;// 将指针 cur 移动到下一个节点}pq->phead = pq->ptail = NULL;// 将队列的头指针和尾指针置为空pq->size = 0;// 将队列的大小置为0pthread_mutex_unlock(&pq->mutex);
}
void MutiThreadCharQueuePush(MutiThreadCharQueue* pq, unsigned char x)
{pthread_mutex_lock(&pq->mutex);MutiThreadCharQueNode* newnode = (MutiThreadCharQueNode*)malloc(sizeof(MutiThreadCharQueNode));// 创建一个新的节点if (newnode == NULL){pthread_mutex_unlock(&pq->mutex);return;}newnode->data = x;// 设置新节点的数据为传入的元素值newnode->next = NULL;// 将新节点的指针域置空//一个节点if (pq->ptail == NULL)// 判断队列是否为空{pq->phead = pq->ptail = newnode;// 将新节点同时设置为队列的头节点和尾节点}//多个节点else{pq->ptail->next = newnode;// 将新节点同时设置为队列的头节点和尾节点pq->ptail = newnode;// 更新队列的尾指针为新节点}pq->size++;// 增加队列的大小计数pthread_mutex_unlock(&pq->mutex);
}
unsigned char MutiThreadCharQueueFront(MutiThreadCharQueue* pq)
{
//    if(QueueEmpty(pq))
//    {
//        return ;
//    }pthread_mutex_lock(&pq->mutex);char data=pq->phead->data;// 返回队列头节点的数据pthread_mutex_unlock(&pq->mutex);return data;
}
void MutiThreadCharQueuePop(MutiThreadCharQueue* pq)
{pthread_mutex_lock(&pq->mutex);//1.一个节点if (pq->phead->next == NULL) // 队列只有一个节点的情况{free(pq->phead); // 释放队列头节点的内存pq->phead = pq->ptail = NULL;// 将队列的头指针和尾指针置为空}//2.多个节点else{MutiThreadCharQueNode* next = pq->phead->next; //保存队列头节点的下一个节点指针free(pq->phead);// 释放队列头节点的内存pq->phead = next;// 更新队列的头指针为下一个节点}pq->size--;//减少队列的大小计数pthread_mutex_unlock(&pq->mutex);
}
void* thread_send(void* para)
{printf("hh\n");MutiThreadCharQueueInit(&TestMutiThreadQue);unsigned char sendChar=1;while(1){printf("send a\n");MutiThreadCharQueuePush(&TestMutiThreadQue,sendChar);usleep(1000000);}
}
void* thread_rev(void* para)
{printf("h2\n");unsigned char revChar;while(1){if(false==MutiThreadCharQueueEmpty(&TestMutiThreadQue)){revChar=MutiThreadCharQueueFront(&TestMutiThreadQue);printf("rev char= %d\n",(int)revChar);MutiThreadCharQueuePop(&TestMutiThreadQue);}usleep(1000000);}
}void create_c_thread_send()
{int ret;pthread_attr_t attr;ret = pthread_attr_init(&attr);if (ret != 0) {return ;}//2pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);int err;pthread_t tid;err = pthread_create(&tid, &attr, thread_send, (void*)NULL);}
void create_c_thread_rev()
{int ret;pthread_attr_t attr;ret = pthread_attr_init(&attr);if (ret != 0) {return ;}//2pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);int err;pthread_t tid;err = pthread_create(&tid, &attr, thread_rev, (void*)NULL);}
int main(int argc, char** argv)
{printf("hello\n");create_c_thread_send();create_c_thread_rev();while(1){usleep(10000);}return 0;
}

3.实现任意数据类型的多线程队列

以上的队列数据类型固定了,希望实现一个通用的多线程队列,并且数据可以得到释放。

#include "stdio.h"
#include <thread>
#include <unistd.h>
#include <pthread.h>
#include <string.h>typedef struct MutiThreadQueNode
{void* data;struct MutiThreadQueNode* next;
}MutiThreadQueNode;typedef struct MutiThreadQueue
{MutiThreadQueNode* phead;MutiThreadQueNode* ptail;int size;int data_mem_size;pthread_mutex_t mutex;
}MutiThreadQueue;typedef struct TestMyStructData
{int my_int_data;float my_float_data;
}TestMyStructData;MutiThreadQueue TestMutiThreadQue;void MutiThreadQueueInit(MutiThreadQueue* pq,int data_mem_size)
{pq->phead=NULL; //将队列的头指针置为空pq->ptail = NULL;//将队列的尾指针置为空pq->size = 0;// 将队列的头指针置为空pq->data_mem_size=data_mem_size;pthread_mutex_init(&pq->mutex, NULL);
}
bool MutiThreadQueueEmpty(MutiThreadQueue* pq)
{pthread_mutex_lock(&pq->mutex);bool bEmpty=(bool) (pq->size == 0);pthread_mutex_unlock(&pq->mutex);return bEmpty;
}
void MutiThreadQueueDestroy(MutiThreadQueue* pq)
{pthread_mutex_lock(&pq->mutex);MutiThreadQueNode* cur = pq->phead;// 创建一个指针 cur,指向队列的头指针while (cur){MutiThreadQueNode* next = cur->next;// 创建一个指针 cur,指向队列的头指针//!由于data是拷贝过来的,释放data内存free(cur->data);free(cur);// 释放当前节点的内存cur = next;// 将指针 cur 移动到下一个节点}pq->phead = pq->ptail = NULL;// 将队列的头指针和尾指针置为空pq->size = 0;// 将队列的大小置为0pthread_mutex_unlock(&pq->mutex);
}
void MutiThreadQueuePush(MutiThreadQueue* pq, void* data,int data_mem_size)
{pthread_mutex_lock(&pq->mutex);MutiThreadQueNode* newnode = (MutiThreadQueNode*)malloc(sizeof(MutiThreadQueNode));// 创建一个新的节点if (newnode == NULL){pthread_mutex_unlock(&pq->mutex);return;}if(pq->data_mem_size!=data_mem_size){printf("input data error\n");pthread_mutex_unlock(&pq->mutex);return;}void* queData=malloc(pq->data_mem_size);memcpy(queData,data,pq->data_mem_size);newnode->data = queData;// 设置新节点的数据为传入的元素值newnode->next = NULL;// 将新节点的指针域置空//一个节点if (pq->ptail == NULL)// 判断队列是否为空{pq->phead = pq->ptail = newnode;// 将新节点同时设置为队列的头节点和尾节点}//多个节点else{pq->ptail->next = newnode;// 将新节点同时设置为队列的头节点和尾节点pq->ptail = newnode;// 更新队列的尾指针为新节点}pq->size++;// 增加队列的大小计数pthread_mutex_unlock(&pq->mutex);
}
void MutiThreadQueueFront(MutiThreadQueue* pq,void* outData,int data_mem_size)
{
//    if(QueueEmpty(pq))
//    {
//        return ;
//    }pthread_mutex_lock(&pq->mutex);if(data_mem_size!=pq->data_mem_size){printf("input data_mem_size error\n");pthread_mutex_unlock(&pq->mutex);return ;}memcpy(outData,pq->phead->data,pq->data_mem_size);pthread_mutex_unlock(&pq->mutex);}
void MutiThreadQueuePop(MutiThreadQueue* pq)
{pthread_mutex_lock(&pq->mutex);//1.一个节点if (pq->phead->next == NULL) // 队列只有一个节点的情况{free(pq->phead); // 释放队列头节点的内存pq->phead = pq->ptail = NULL;// 将队列的头指针和尾指针置为空}//2.多个节点else{MutiThreadQueNode* next = pq->phead->next; //保存队列头节点的下一个节点指针//!由于data是拷贝过来的,释放data内存free(pq->phead->data);free(pq->phead);// 释放队列头节点的内存pq->phead = next;// 更新队列的头指针为下一个节点}pq->size--;//减少队列的大小计数pthread_mutex_unlock(&pq->mutex);
}
void* thread_send(void* para)
{printf("hh\n");TestMyStructData mySendData;mySendData.my_int_data=1;mySendData.my_float_data=2;MutiThreadQueueInit(&TestMutiThreadQue,sizeof(TestMyStructData));while(1){printf("send 1\n");MutiThreadQueuePush(&TestMutiThreadQue,&mySendData,sizeof(TestMyStructData));usleep(1000000);}
}
void* thread_rev(void* para)
{printf("h2\n");TestMyStructData myRevData;while(1){if(false==MutiThreadQueueEmpty(&TestMutiThreadQue)){MutiThreadQueueFront(&TestMutiThreadQue,&myRevData,sizeof(TestMyStructData));printf("rev intdata= %d float data=%f\n",myRevData.my_int_data,myRevData.my_float_data);MutiThreadQueuePop(&TestMutiThreadQue);}usleep(1000000);}
}void create_c_thread_send()
{int ret;pthread_attr_t attr;ret = pthread_attr_init(&attr);if (ret != 0) {return ;}//2pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);int err;pthread_t tid;err = pthread_create(&tid, &attr, thread_send, (void*)NULL);}
void create_c_thread_rev()
{int ret;pthread_attr_t attr;ret = pthread_attr_init(&attr);if (ret != 0) {return ;}//2pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);int err;pthread_t tid;err = pthread_create(&tid, &attr, thread_rev, (void*)NULL);}
int main(int argc, char** argv)
{printf("hello\n");create_c_thread_send();create_c_thread_rev();while(1){usleep(10000);}return 0;
}

4.队列其他操作

队列操作可以完善的点
1.加上队列最大限制,如果队列内数据大小超过阈值,清空队列

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/222422.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++笔记之system()用于在Qt中执行系统命令的习惯

C笔记之system()用于在Qt中执行系统命令的习惯 参考博文&#xff1a;qt-C笔记之std::tostring()、.toStdString()、.toLocal8Bit().constData()的使用场景 code review! 文章目录 C笔记之system()用于在Qt中执行系统命令的习惯一.一般我用的int system( const char *command…

HarmonyOS学习0基础版

1.安装并配置DevEco 访问 HUAWEI开发者官网 找到 DevEco点击下载,我这里以windows版为例 点击下载并安装 (安装时直接点击下一步下一步,然后运行安装好的DevEco) 注意&#xff1a;第一次安装没有开发环境的时候&#xff0c;这里点击Do not import settings&#xff0c;进入软…

modelbox线程爆满宕机bug

序 该bug的解决需要特别感谢张同学。有了大佬的帮助&#xff0c;这个bug才得以解决。 问题现象 modelbox可以进行模型推理&#xff0c;但压测一段时间后&#xff0c;modelbox会宕机&#xff0c;并发生段错误。 “libgomp: Thread creation failed: Resource temporarily una…

MacOS多屏状态栏位置不固定,程序坞不小心跑到副屏

目录 方式一&#xff1a;通过系统设置方式二&#xff1a;鼠标切换 MacOS多屏状态栏位置不固定&#xff0c;程序坞不小心跑到副屏 方式一&#xff1a;通过系统设置 先切换到左边 再切换到底部 就能回到主屏了 方式二&#xff1a;鼠标切换 我的两个屏幕放置位置如下 鼠标在…

OpenCV imencode 函数详解与应用示例

OpenCV imencode 函数详解与应用示例 介绍imencode 函数的基本信息示例代码应用场景 介绍 OpenCV是一个强大的计算机视觉库&#xff0c;提供了许多图像处理和分析的工具。imencode函数是其中之一&#xff0c;用于将图像编码为指定格式的字节流。这个函数对于图像的存储、传输和…

实验03:OSPF配置网络实验

1.实验目的&#xff1a; 本实验的主要目的是了解OSPF协议的基本概念、OSPF网络的配置及验证&#xff0c;通过实验来掌握OSPF协议的工作原理、配置方法、路由表的生成过程等。 2.实验内容&#xff1a; 设计一个拓扑结构&#xff0c;并在网络设备上进行配置&#xff1b;配置OS…

架构简洁之道有感,谈谈软件组件聚合的张力

配图由腾讯混元助手生成 这篇文章介绍了软件架构设计中组件设计思想&#xff0c;围绕“组件间聚合的张力”这个有意思的角度&#xff0c;介绍了概念&#xff0c;并且结合架构设计示例对这个概念进行了进一步阐述。 组件聚合&#xff1f;张力&#xff1f;这标题&#xff0c;有种…

华为HCIP认证H12-821题库下

26、6.交换技术核心知识 &#xff08;单选题&#xff09;某交换机运行RSTP协议&#xff0c;其相关配置信息如图所示,请根据命令配置情况指出对于Instance 1&#xff0c;该交换机的角色是: A、根交换机 B、非根交换机 C、交换机 D、无法判断 正确答案是&…

本地计算机连接两个Github账号

两个Github账号与本地计算机连接 注册Github账号Git的下载与安装生成SSH密钥为Github账户设置SSH Key编辑config文件连接Github仓库其它命令 注册Github账号 注册两个Github账号。这一步很简单&#xff0c;跟注册其它账号差不多。 Git的下载与安装 下载地址&#xff1a;http…

在C++中->运算符

在C中&#xff0c;->是一个运算符&#xff0c;称为成员访问运算符&#xff08;Member Access Operator&#xff09;。它用于通过指针访问对象的成员&#xff08;变量或函数&#xff09;。 下面是对->运算符的详细解释&#xff1a; 语法&#xff1a; pointer->member …

汽车锁行业分析:市场销量接近1700万台

汽车防盗锁根据技术原理基本上可划分为三类&#xff1a;机械防盗锁、电子防盗报警锁、联网的防盗抢(定位、跟踪)系统。汽车发动机防盗逻辑&#xff0c;点火开关打开时&#xff0c;钥匙转发器与防盗控制器形成首次信息交汇&#xff0c;钥匙与芯片互会识别码不同&#xff0c;防盗…

SQAlchemy 第二篇

使用数据库元数据 SQLAlchemy 中数据库元数据最常见的基础对象称为 MetaData、Table和Column。下面的部分将说明如何在面向 Core 的风格和面向 ORM 的风格中使用这些对象。 使用表对象设置元数据 当我们使用关系数据库时&#xff0c;我们查询的数据库中的基本数据保存结构称…

php使用OpenCV实现从照片中截取身份证区域照片

<?php // 获取上传的文件 $file $_FILES[file]; // 获取文件的临时名称 $tmp_name $file[tmp_name]; // 获取文件的类型 $type $file[type]; // 获取文件的大小 $size $file[size]; // 获取文件的错误信息 $error $file[error]; // 检查文件是否上传成功 if ($er…

Linux 服务管理和配置

这篇主要是围绕 systemd 的管理和配置&#xff0c;有新的认识会持续更新 一、服务管理 Centos 7 及后续版本 systemd # 无需重启&#xff0c;重新加载服务 一般用于配置文件变更后 systemctl reload daemon # 服务的开机自启动管理 systemctl enable/disable daemon # 服务状…

阶段五:深度学习和人工智能(学习人工智能的应用领域,如自然语言处理,计算机视觉等)

Python是人工智能领域最流行的编程语言之一&#xff0c;它具有简单易学、功能强大、库丰富等优点&#xff0c;因此在自然语言处理、计算机视觉等领域得到了广泛应用。 自然语言处理 自然语言处理是人工智能领域的一个重要分支&#xff0c;它主要研究如何让计算机理解和处理人…

Matlab绘图添加背景色,动态添加背景

Matlab绘图添加背景色&#xff0c;动态添加背景 有没有小伙伴想过绘制这种有背景的曲线图呢&#xff1f;因为矩形是背景&#xff0c;所以要先绘制&#xff0c;然后再绘制曲线&#xff0c;因此&#xff0c;最先想到的思路可能是&#xff1a;先绘制三个背景矩形&#xff0c;然后填…

编译Sqlite3记录

下载源文件&#xff1a; 下载地址&#xff1a;SQLite Download Page 打开QtCreator创建新的工程&#xff0c;选择纯C工程&#xff0c;将main.c删除&#xff0c;将下载的源码解压后的文件复制到并添加到工程中&#xff0c;其中的文件包括&#xff1a;sqlite3ext.h、sqlite3.h、…

云原生之深入解析网络服务Istio、eBPF和RSocket Broker

一、服务治理 ① “服务治理”简介 在微服务时代&#xff0c;一个复杂的应用程序被分解为多个组件化、协作和连接的单元&#xff0c;服务往往会承担越来越多的业务责任&#xff0c;这使得服务治理的难度前所未有&#xff0c;仅仅依靠微服务框架级的治理是不够的&#xff0c;构…

vivado约束方法4

时序约束向导 定时约束向导确定合成或上缺少的定时约束实现的设计。它分析了网表、时钟网络连接和现有的定时限制&#xff0c;以便根据《超快设计方法指南》提供建议用于FPGA和SoC&#xff08;UG949&#xff09;。以下11涵盖了三类约束页面&#xff0c;然后是摘要。包括以下步…

uni-app地图标点展示

使用微信小程序框架编写的。它包含一个模板部分和一个脚本部分。 模板部分: <template>: 这是微信小程序中用来定义页面结构的标签。<view>: 微信小程序的基本组件&#xff0c;用来展示内容或布局。<view class"page-body">: 定义了一个页面主体部…