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

11.4 Linux_线程_条件变量

概述

条件变量的作用:

条件变量和互斥量配合使用,主要应用于生产者和消费者问题。

这种问题也是一种临界资源的问题,但与互斥量一文中 "写文件" 这种资源不同。文件是一直存在的临界资源,而生产者的资源不是一直存在,资源的产生需要一些条件。条件变量实现了在等待生产者时,消费者进入休眠状态,提高运行效率。

条件变量使用方法:

1、初始化互斥量、条件变量

2.1 生产者:加锁互斥量->生产资源->发送信号给条件变量->解锁互斥量

2.2 消费者:加锁互斥量->无资源时等待条件变量->消费资源->解锁互斥量

相关函数

1、初始化

//动态初始化
int pthread_cond_init(pthread_cond_t *restrict cond,const pthread_condattr_t *restrict attr);
//静态初始化
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

返回值:成功返回0,失败返回错误码

cond:要初始化的条件变量

attr:条件变量属性,NULL代表默认属性

2、销毁 

int pthread_cond_destroy(pthread_cond_t *cond);

返回值:成功返回0,失败返回错误码

cond:要销毁的条件变量

3、等待资源

//等待临界资源,阻塞
int pthread_cond_wait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex);
//等待临界资源,一段时间后退出阻塞
int pthread_cond_timedwait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex,const struct timespec *restrict abstime);

cond:等待哪一个条件变量的信号

mutex:与条件变量搭配使用的互斥量

注意:该函数在没有资源时,会解锁互斥量,并把本线程休眠。当接收到资源时,会解除休眠,并再次把互斥量上锁

4、发送信号 

//单个发送信号,代表资源来了,所有线程竞争该资源
int pthread_cond_signal(pthread_cond_t *cond);
//广播发送信号,代表资源来了
int pthread_cond_broadcast(pthread_cond_t *cond);

cond:要向哪一个条件变量发送信号

注意:当 "发送信号" 比 "等待资源" 早时,"等待资源" 是接收不到信号的,这会导致信号丢失。这意味着当运行到 "等待资源" 时,会进入阻塞,直到第二次 "发送信号" 到来

生产者消费者示例代码

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
pthread_mutex_t mutex;//互斥锁
pthread_cond_t cond;//条件变量
//资源
struct resource{char num;struct resource* pNext;
};
struct resource Head;
struct resource* pTail;
//消费者
void* consumer(void* arg){struct resource* pResource = NULL;while(1){//1.加锁互斥量pthread_mutex_lock(&mutex);//2.无资源时等待条件变量,有资源时不断消耗资源while(Head.pNext == NULL){//while的作用:防止信号为广播时产生惊群效应pthread_cond_wait(&cond,&mutex);}//3.消费资源pResource = Head.pNext;Head.pNext = Head.pNext->pNext;if(Head.pNext == NULL){pTail = &Head;}printf("consumer:num = %d\n",pResource->num);free(pResource);//4.解锁互斥量pthread_mutex_unlock(&mutex);sleep(1);}
}
//生产者
void* producer(void* arg){struct resource* pNew = NULL;int i = 0;while(1){//1.加锁互斥量pthread_mutex_lock(&mutex);//2.生产资源if((pNew = malloc(sizeof(struct resource))) == NULL){printf("malloc err\n");pthread_exit(NULL);}pNew->num = i++;pNew->pNext = NULL;pTail->pNext = pNew;pTail = pNew;printf("producer:num = %d\n",pNew->num);//3.发送信号给条件变量pthread_cond_signal(&cond);//4.解锁互斥量pthread_mutex_unlock(&mutex);sleep(1);}
}
int main(){pthread_t tid;Head.pNext = NULL;pTail = &Head;//初始化互斥锁和条件变量pthread_mutex_init(&mutex,NULL);pthread_cond_init(&cond,NULL);//创建线程if(pthread_create(&tid,NULL,producer,NULL) != 0){perror("pthread_create");pthread_detach(tid);return -1;}if(pthread_create(&tid,NULL,consumer,NULL) != 0){perror("pthread_create");return -1;}while(1);//销毁互斥锁和条件变量pthread_mutex_destroy(&mutex);pthread_cond_destroy(&cond);return 0;
}

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

相关文章:

  • 通信工程学习:什么是IP网际协议
  • github 国内文件加速下载
  • 算法6:模拟运算
  • 【网络协议大花园】应用层 http协议的使用小技巧,用好了都不用加班,效率翻两倍(上篇)
  • 今日指数day8实战补充(上)
  • Python 之进阶语法:with...as...
  • 嵌入式硬件设计知识详解
  • 计算机网络:物理层 —— 信道及其极限容量
  • 面向对象特性中 继承详解
  • C++ | Leetcode C++题解之第455题分发饼干
  • java版基于Spring Boot + Mybatis在线招投标|评标|竞标|单一采购|询价|邀标|在线开标|招标公告发布|评审专家|招投标采购系统源码
  • Anaconda的安装与环境设置
  • 使用FastAPI做人工智能后端服务器时,接口内的操作不是异步操作的解决方案
  • Leetcode 3312. Sorted GCD Pair Queries
  • 用 Delphi 做了一个简单的 CMS
  • ASK, PSK, FSK, DPSK
  • 【Linux】认识Linux内核中进程级别的文件结构体【files_struct】&文件IO模型初步演示
  • [Offsec Lab] ICMP Monitorr-RCE+hping3权限提升
  • Studying-多线程学习Part4 - 异步并发——async future、packaged_task、promise
  • 【Java基础】用Scanner类获取控制台输入
  • 微服务seata解析部署使用全流程
  • Linux性能调优技巧
  • python 实现sha1算法
  • ejb-ref元素
  • Perl 子程序(函数)
  • ElasticSearch 备考 -- Snapshot Restore
  • 【Linux】进程替换、命令行参数及环境变量(超详解)
  • MySQL事务日志—redo日志介绍
  • 告别音乐小白!字节跳动AI音乐创作工具,让你一键变作曲家!
  • 空心正方形图案