当前位置: 首页 > news >正文

线程条件变量 生产者消费者模型 Linux环境 C语言实现

只能用来解决同步问题,且不能独立使用,必须配合互斥锁一起用


头文件:#include <pthread.h>
类型:pthread_cond_t
PTHREAD_COND_INITIALIZER 初始化
初始化:int pthread_cond_init(pthread_cond_t * cond, NULL);
清理:int pthread_cond_destroy(pthread_cond_t * cond);
P操作: int pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t *mutex);//mutex必须是pthread_mutex_lock返回后的
V操作: int pthread_cond_signal(pthread_cond_t * cond);//只是出队任务等待队列中的第一个任务,让该任务正在阻塞的P操作返回
V操作: int pthread_cond_broadcast(pthread_cond_t * cond);//出队任务等待队列中的所有任务,让所有正阻塞的任务的P操作返回功能:见函数名
返回值:成功0,失败错误码
mutex为已经处于锁状态的互斥锁,因此条件变量必须与一个互斥量配合使用


代码模板:

代码模板:
pthread_cond_t cond;
pthread_mutex_t lock;程序开始时:
pthread_mutex_init(&lock,NULL);
pthread_cond_init(&cond,NULL);程序结束前:
pthread_mutex_destroy(&lock);
pthread_cond_destroy(&cond);A线程为先做线程
pthread_mutex_lock(&lock);
....
pthread_mutex_unlock(&lock);
pthread_cond_signal(&cond);B线程为后做线程
pthread_mutex_lock(&lock);
if(条件表达式为真)
{pthread_cond_wait(&cond,&lock);
}
.....
pthread_mutex_unlock(&lock);

生产者消费者模型(生产者生产出来东西后消费者才能执行)

问题:设计程序,一个线程实现入队操作,另一个实现出队操作, 如果队列中没有数据,出队线程不参与资源的竞争.(条件变量)
代码:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>#if  0
条件变量在生产者消费者模型中使用步骤:
1、定义条件变量和互斥锁,并初始化消费者线程:
1、加锁int pthread_mutex_lock(pthread_mutex_t *mutex);2、判断公共资源中是否有数据
while(公共资源中没有数据成立)
{//造成线程的阻塞,在阻塞之前进行解锁//被唤醒后,申请加锁int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);}3、如果公共资源中有数据, 消费数据4、解锁
int pthread_mutex_unlock(pthread_mutex_t *mutex);生产者线程:
1、加锁int pthread_mutex_lock(pthread_mutex_t *mutex);2、生产数据, 并放到公共资源中3、解锁
int pthread_mutex_unlock(pthread_mutex_t *mutex);4、发送信号给条件变量, 唤醒所有阻塞在条件变量上的线程int pthread_cond_signal(pthread_cond_t *cond);#endifstruct  node{int data;struct node *next;
};pthread_cond_t cond = PTHREAD_COND_INITIALIZER; // 初始化cond条件变量
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 初始化mutex锁struct node *head = NULL;//生产者线程
void* product(void* argv){struct node *pnew = NULL;// sleep(10);while(1){pnew = malloc(sizeof(struct node));assert(pnew != NULL);pnew->data = rand();pnew->next = NULL;printf("========product: %d\n", pnew->data);pthread_mutex_lock(&mutex); // 上锁pnew->next = head;head = pnew;pthread_mutex_unlock(&mutex); // 解锁pthread_cond_signal(&cond); // 发出信号sleep(1);}return NULL;
}//消费者线程
void*  consumer(void* argv){struct node *pdel = NULL;while(1){pthread_mutex_lock(&mutex); // 上锁while(NULL == head){ // 判断条件是否满足printf("wait..\n");pthread_cond_wait(&cond, &mutex); // 解锁等待,条件满足后再上锁printf("wait  over...\n");}printf("------->consumer: %d\n", head->data);pdel = head;head = head->next;free(pdel);pthread_mutex_unlock(&mutex); // 解锁sleep(1);}return NULL;
}int main(){pthread_t  pth1, pth2;if(0 != pthread_create(&pth1, NULL, product, NULL)){perror("pthread_create");return -1;}if(0 != pthread_create(&pth2, NULL, consumer, NULL)){perror("pthread_create");return -1;}pthread_join(pth1, NULL); // 回收线程pthread_join(pth2, NULL);pthread_mutex_destroy(&mutex); // 销毁锁pthread_cond_destroy(&cond); // 销毁条件变量return 0;
}

输出:

http://www.lryc.cn/news/497376.html

相关文章:

  • C++ packaged_task
  • 【联表查询】.NET开源 ORM 框架 SqlSugar 系列
  • 嵌入式C编程:宏定义与typedef的深入对比与应用
  • 高级java每日一道面试题-2024年12月03日-JVM篇-什么是Stop The World? 什么是OopMap? 什么是安全点?
  • 【openGauss︱PostgreSQL】openGauss或PostgreSQL查表、索引、序列、权限、函数
  • Dataset用load_dataset读图片和对应的caption的一个坑
  • 【信息系统项目管理师】第7章:项目立项管理 考点梳理
  • 知识库、提示词对大语言模型的影响测试
  • vistat-监控和分析网络状态
  • EasyAnimateV5 视频生成大模型原理详解与模型使用
  • 水稻和拟南芥生命周期中单碱基分辨率的m6A定量分析-文献精读88
  • 学习threejs,使用canvas更新纹理
  • 【笔记2-3】ESP32 bug:PSRAM chip not found or not supported 没有外部PSRAM问题解决
  • 大数据hadoop、spark、flink、kafka发展的过程
  • 设计模式-理论基础
  • 猎板 PCB特殊工艺:铸就电子行业核心竞争力新高度
  • 用于目标检测的集中式特征金字塔
  • 大舍传媒-关于海外媒体宣发的探讨
  • Python从入门到入狱
  • AMEYA360 | 杭晶电子:晶振在AR/VR中的应用
  • RAG评估指南:从检索到生成,全面解析LLM性能评估方法
  • 贪心算法实例-问题分析(C++)
  • Ubuntu20.04 配置虚拟显示器和切回物理显示器
  • HTML 常用标签属性汇总一〈body〉标签
  • Python yield关键字
  • tomcat的Mysql链接字符串问题
  • 聊聊JVM G1(Garbage First)垃圾收集器
  • 【论文复现】隐式神经网络实现低光照图像增强
  • Python知识分享第十九天-网络编程
  • C# 绘制GDI红绿灯控件