Linux之线程条件变量cond
概念:条件变量不是锁,要和互斥量组合使用。条件变量就是生产者“生产”完成,消费者才能“使用”,如果没有“产品”,消费者就会被条件变量cond阻塞等待生产者“生产”。(生产者与消费者模型)
函数:
- 1.超时等待
int pthread_cond_timedwait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex,const struct timespec *restrict abstime);
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
struct timespec{
time_t tv_sec; /*seconds*/秒
long tv_nsec; /*nanoseconds*/纳秒
}
tv_sec 绝对时间,填写的时候,例如time(NULL)+600,表示设置超时600s。
- 2.条件变量阻塞等待
int pthread_cond_wait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex);
先释放锁mutex;
阻塞在cond条件变量上。
- 3.初始化一个条件变量
int pthread_cond_init(pthread_cond_t *restrict cond,const pthread_condattr_t *restrict attr);pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
- 4.销毁一个条件变量
int pthread_cond_destroy(pthread_cond_t *cond);
- 5.信号,唤醒至少一个阻塞在条件变量cond上的线程
int pthread_cond_signal(pthread_cond_t *cond);
- 6.唤醒阻塞在条件变量cond上的全部线程
int pthread_cond_broadcast(pthread_cond_t *cond);
示例:创建两个线程,一个线程为生产者,两个线程为消费者。(生产者与消费者模型)
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>int beginnum = 1000;//初始化互斥锁和条件变量
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;//定义一个链表,用于存取数据
typedef struct _ProdInfo
{int num;struct _ProdInfo *next;
}ProdInfo;ProdInfo *Head = NULL;void *thr_producter(void *arg)
{//负责在链表中添加数据while (1){ProdInfo * prod = malloc(sizeof(ProdInfo));prod->num = beginnum++;printf("----%s----self = %lu----%d\n",__FUNCTION__,pthread_self(),prod->num);pthread_mutex_lock(&mutex);//添加到链表prod->next = Head;Head = prod;pthread_mutex_unlock(&mutex);//发起通知pthread_cond_signal(&cond);sleep(rand()%2);//free(prod); //重复释放内存会报错,用完在释放}return NULL;
}void *thr_customer(void *arg)
{ProdInfo *prod = NULL;while (1){//获取链表中的数据pthread_mutex_lock(&mutex);//if(Head = NULL)while (Head == NULL){pthread_cond_wait(&cond,&mutex); //在此之前必须加锁}prod = Head;Head = Head->next;printf("----%s----self = %lu----%d\n",__FUNCTION__,pthread_self(),prod->num);pthread_mutex_unlock(&mutex);sleep(rand()%4);free(prod);}return NULL;
}int main()
{pthread_t tid[3];pthread_create(&tid[0],NULL,thr_producter,NULL);pthread_create(&tid[1],NULL,thr_customer,NULL);pthread_create(&tid[2],NULL,thr_customer,NULL);pthread_join(tid[0],NULL);pthread_join(tid[1],NULL);pthread_join(tid[2],NULL);pthread_mutex_destroy(&mutex);pthread_mutex_destroy(&cond);return 0;
}
输出:
~$ gcc cond_product.c -lpthread
~$ ./a.out
----thr_producter----self = 140197865371392----1000
----thr_customer----self = 140197856978688----1000
----thr_producter----self = 140197865371392----1001
----thr_customer----self = 140197731759872----1001
----thr_producter----self = 140197865371392----1002
----thr_customer----self = 140197856978688----1002
----thr_producter----self = 140197865371392----1003
----thr_producter----self = 140197865371392----1004
----thr_producter----self = 140197865371392----1005
----thr_customer----self = 140197731759872----1005
----thr_producter----self = 140197865371392----1006
----thr_producter----self = 140197865371392----1007
----thr_customer----self = 140197856978688----1007
----thr_customer----self = 140197731759872----1006
Linux之线程条件变量cond相关推荐
- linux条件变量cond,Linux C 条件变量cond的使用记录
条件变量是实现线程间同步的一种方法,条件变量用来自动阻塞一个线程,直到收到收到一个cond信号或其它特殊情况发送,条件变量使用的时候必须与互斥量同时使用,这是为了保证条件量在线程间操作的"原 ...
- Go语言编程:使用条件变量Cond和channel通道实现多个生产者和消费者模型
如题,使用条件变量Cond和channel通道实现多个生产者和消费者模型.Go语言天生带有C语言的基因,很多东西和C与很像,但是用起来 绝对比C语言方便.今天用Go语言来实现下多消费者和生产者模型.如 ...
- linux中的条件变量的使用
什么是条件变量 条件变量是利用线程间共享的全局变量进行同步的一种机制. 主要包括两个动作:一个线程等待"条件变量的条件成立"而挂起:另一个线程使"条件成立"(给 ...
- linux操作系统之条件变量
(1)条件变量 条件变量本身不是锁,但它可以造成线程阻塞,通常与互斥锁配合使用. (2)条件锁相关函数 pthread_cond_t类型,用于定义条件变量 1)初始化一个条件变量:pthread_co ...
- Go语言中的条件变量Cond
一.条件变量Cond的定义 Go语言里的条件变量,是一个结构体,它包括对应的方法和属性字段. Cond实现了⼀个条件变量,⼀个线程集合地,供线程等待或者宣布某事件的发⽣. 每个Cond实例都有⼀个相关 ...
- linux多线程之条件变量
1.条件变量概述: 条件变量是用来等待线程而不是上锁的,条件变量通常和互斥锁一起使用.条件变量之所以要和互斥锁一起使用,主要是因为互斥锁的一个明显的特点就是它只有两种状态:锁定和非锁定,而条件变量可以 ...
- Python 线程条件变量 Condition - Python零基础入门教程
目录 一.Python 线程条件变量 Condition 函数 二.Python 线程条件变量 Condition 原理 三.Python 线程条件变量 Condition 使用 四.Python 线 ...
- linux条件变量cond,Linux 条件变量 pthread_cond_signal及pthread_cond_wait
#include #include #include #include pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;/*静态初始化*/ pthr ...
- Linux多线程同步------条件变量
先来看下<Linux高性能服务器编程>中对条件变量的描述: 上述话可以总结为: 多线程中某一个线程依赖于另外一个线程对共享数据的改变时,就可以使用条件变量! 用消费者生产者的来理解条件变量 ...
最新文章
- MongoDB数据导入hbase + 代码
- 爱立信数据分析解决方案抓住物联网发展机遇
- 牛逼!mysql创建库books
- 前端折线图中背景,Chart.js折线图设置背景颜色
- cgroup的学习(一)——what cgroup?
- linux下磁盘及文件系统基础知识(1)
- 电子计算机为什么123安不出来,方正软件常见问题及其解决办法-精.doc
- MIR7 金额计算公式
- Photoshop:如何使图片覆盖在文字上以及一种海报效果实现
- linux搭建云存储服务,CentOS 6.3搭建个人私有云存储ownCloud
- cf两边黑屏怎么解决win10_Win10/7系统进入LOL英雄联盟显示输入不支持并黑屏原因及解决方法...
- 阿里云被攻击封多久,又该怎么解决?
- UG 信息窗口弹不出来 测量 长度 角度 信息 窗口 弹不出来
- 如何输出一个国际象棋棋盘
- Flutter无限循环滑动的PageView
- linux centos7.x 编译安装php7.4.2
- svn:svnserve 配置使用
- 子类继承多个父类总结
- spss基础-5.10
- 为了给YiYi节省时间,写了个能自动拼图贴水印的机器人,很多bug,能用就行。
热门文章
- codeforces上的名字颜色和codeforces打比赛转载
- 资源依赖项注入失败: org.springframework.beans.factory.BeanCreationException: Error creating bean with name
- AutoCAD 2004-2022 官方简体中文版下载直链
- Word目录排版,页码格式转换
- 必看!前辈们总结出的程序员找工作遇到的坑
- 一款专为APP设计的后台管理系统平台
- Fastadmin 自带的导入Excel功能
- 高考加油的c语言程序,2020祝高三高考加油的句子 高考加油一句话
- Linux篇-The slave I/O thread stops because master and slave have equal...
- vivo手机html有吗,vivo手机有哪些系列?区别是什么?