【linux高级程序设计】(第十二章)Linux多线程编程 3
条件变量通信机制
条件变量必须配合互斥锁一起实现对资源的互斥访问
使用方法:

int pthread_cond_init (pthread_cond_t *__restrict __cond, __const pthread_condattr_t *__restrict __cond_attr) :初始化条件变量,第二个参数是属性对象,NULL为默认
int pthread_cond_destroy (pthread_cond_t *__cond) :销毁条件变量
int pthread_cond_signal (pthread_cond_t *__cond) :通知等待条件变量的第一个线程,如果没有等待线程,则不起作用
int pthread_cond_broadcast (pthread_cond_t *__cond) :通知等待的条件变量的所有线程,如果没有等待线程,则不起作用
int pthread_cond_wait (pthread_cond_t *__restrict __cond, pthread_mutex_t *__restrict __mutex) :等待条件变量,第二个参数指向于条件变量关联的互斥锁指针
int pthread_cond_timewait (pthread_cond_t *__restrict __cond, pthread_mutex_t *__restrict __mutex, __const struct timespec *__restrict __abstime) :在指定时间内等待条件变量。
在线程因等待条件变量进入等待状态时,将释放其申请的互斥锁。在等到条件变量时,会隐含申请到该互斥锁
条件变量实现读写锁
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include "pthread.h"
#define BUFFER_SIZE 2
//条件信息结构体
struct prodcons
{
int buffer[BUFFER_SIZE]; //生产产品值
pthread_mutex_t lock; //互斥锁
int readpos, writepos; //读写位置
pthread_cond_t notempty; //条件变量,表非空
pthread_cond_t notfull; //条件变量,表非满
}; //初始化
void init(struct prodcons *prod)
{
pthread_mutex_init(&prod->lock, NULL);
pthread_cond_init(&prod->notempty, NULL);
pthread_cond_init(&prod->notfull, NULL);
prod->readpos = ;
prod->writepos = ;
} //在缓冲区输入一个数据
void put(struct prodcons * prod, int data)
{
//锁定互斥锁
pthread_mutex_lock(&prod->lock);
while((prod->writepos + ) % BUFFER_SIZE == prod->readpos) //测试是否空间已满 注意 这里writepos的位置要加1 即保证下一个写入的位置是空的 ??
{
printf("producer wait for not full\n");
pthread_cond_wait(&prod->notfull, &prod->lock); //等待有空间写
}
//写数据
prod->buffer[prod->writepos] = data;
prod->writepos++;
if(prod->writepos >= BUFFER_SIZE)
prod->writepos = ;
pthread_cond_signal(&prod->notempty);
pthread_mutex_unlock(&prod->lock);
} //从缓冲区读一个数据
int get(struct prodcons * prod)
{
int data;
//锁定互斥锁
pthread_mutex_lock(&prod->lock);
while(prod->writepos == prod->readpos)
{
printf("producer wait for not empty\n");
pthread_cond_wait(&prod->notempty, &prod->lock);
}
data = prod->buffer[prod->readpos];
prod->readpos++;
if(prod->readpos >= BUFFER_SIZE)
prod->readpos = ;
pthread_cond_signal(&prod->notfull);
pthread_mutex_unlock(&prod->lock);
return data;
} #define OVER (-1)
struct prodcons buffer; //生产者
void * producer(void * data)
{
int n;
for(n = ; n <= ; n++) //生产5个产品
{
printf("producer sleep 1 second...\n");
sleep();
printf("put the %d product\n", n);
put(&buffer, n);
}
for(n = ; n <= ; n++)
{
printf("producer sleep 3 second...\n");
sleep();
printf("put the %d product\n", n);
put(&buffer, n);
}
put(&buffer, OVER);
printf("producer stopped!\n");
return NULL;
} //消费者
void * consumer(void * data)
{
int d = ;
while()
{
printf("consumer sleep 2 second...\n");
sleep();
d = get(&buffer);
printf("get the %d product\n", d);
if(d == OVER)
break;
}
printf("consumer stopped!\n");
return NULL;
} int main(int argc, char *argv[])
{
pthread_t th_a, th_b;
void * retval;
init(&buffer);
pthread_create(&th_a, NULL, producer, );
pthread_create(&th_b, NULL, consumer, );
pthread_join(th_a, &retval);
pthread_join(th_b, &retval);
return ;
}
验证,可用
【linux高级程序设计】(第十二章)Linux多线程编程 3的更多相关文章
- linux高级管理第十二章--rsync
实验部分 1.安装rsync 2.配置文件 3.配置密码 4.后续 5.为了测试,创建几个文件 配置实时同步 1.调整inotify内核参数 安装inotify-tools 测试同步 编写脚本 验证 ...
- 读书笔记 - js高级程序设计 - 第十二章 DOM2和DOM3
Node类型的变化 访问元素的样式 myDiv.style.backgroundColor = "red" myDiv.style.width = "100px& ...
- 疯狂JAVA讲义---第十二章:Swing编程(五)进度条和滑动条
http://blog.csdn.net/terryzero/article/details/3797782 疯狂JAVA讲义---第十二章:Swing编程(五)进度条和滑动条 标签: swing编程 ...
- 鸟哥的linux私房菜——第十二章学习(Shell Scripts)
第十二章 Shell Scripts 1.0).什么是shell scripts? script 是"脚本.剧本"的意思.整句话是说, shell script 是针对 shel ...
- 读书笔记 - js高级程序设计 - 第十五章 使用Canvas绘图
读书笔记 - js高级程序设计 - 第十三章 事件 canvas 具备绘图能力的2D上下文 及文本API 很多浏览器对WebGL的3D上下文支持还不够好 有时候即使浏览器支持,操作系统如果缺缺 ...
- 鸟哥的Linux私房菜——第十二章:档案的压缩与打包
视频链接: 土豆:http://www.tudou.com/programs/view/GncwT0FJKsQ B站(推荐):http://www.bilibili.com/video/av98857 ...
- 第三十二章 Linux常规练习题(一)
一.练习题一 1.超级用户(管理员用户)提示符是____,普通用户提示符是____.2.linux关机重启的命令有哪些 ?3.bash是什么?4.bash特性, 常见的bash特性有哪些?5.网卡的配 ...
- 第十二章 Linux三剑客之老三—grep
一.Linux grep 命令用于查找文件里符合条件的字符串. Linux系统中的grep命令是一种功能强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来.grep全称是Global ...
- 【linux高级程序设计】(第九章)进程间通信-管道 1
Linux操作系统所支持的主要进程间的通信机制. 无名管道 PIPE cat test.txt| grep hello 上面这种管道,将一个命令的输出作为另一个命令的输入,而这种管道是临时的,命令执行 ...
- 第十二章Linux文件系统与日志
1.inode 包含文件的元信息(1)inode 内容:文件的字节数.拥有者的 UID.GID.文件的读写执行权限.时间戳等,但不包含文件名.文件名是储存在目录的目录项中.(2)查看文件的 inode ...
随机推荐
- Java继承的缺点
转载自:https://www.cnblogs.com/xz816111/archive/2018/05/24/9080173.html JAVA中使用到继承就会有两个无法回避的缺点: 1.打破了封装 ...
- ios在tableview里面加subview后在ip4和ip5上显示不一样的问题
文章链接:http://quke.org/post/ios-tableview-addsubview-height.html (转载时请注明本文出处及文章链接) 我在在tableview里面加subv ...
- 《Cracking the Coding Interview》——第16章:线程与锁——题目4
2014-04-27 20:06 题目:设计一个类,只有在不产生死锁的时候才分配资源. 解法:不太清楚这个题是要分配何种资源,以何种形式?所以没能动手写个可运行的代码,只是闲扯了几句理论分析. 代码: ...
- USACO Section1.3 Ski Course Design 解题报告
skidesign解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------ ...
- App自动化测试前期准备---android SDK配置
说明:就是配置android SDK 一.sdk下载 Windows(X64):立即下载 Linux(X64):立即下载 二.Windows配置 1.解压文件 直接解压到指定目录(演示目录:D:/) ...
- linux c编程(一)
1 常用系统环境配置 2 使用g++编译连接,使用gdb调试 3 使用makefile组织目标文件的依赖关系 4 使用git 1 常用系统环境配置 输入法 Download setup file fo ...
- 团队项目-第二次Scrum 会议
时间:10.24 时长:30分钟 地点:线上 工作情况 团队成员 已完成任务 待完成任务 解小锐 学习官方样例 根据初步讨论结果编写初步的api文档 陈鑫 学习cocos creator基本使用 采用 ...
- htmlagilitypack解析html
这是个很好的的东西,以前做Html解析都是在用htmlparser,用的虽然顺手,但解析速度较慢,碰巧今天找到了这个,就拿过来试,一切出乎意料,非常爽,推荐给各位使用. 下面是一些简单的使用技巧,希望 ...
- UVALive 6324 Archery (求射箭覆盖的期望)
#include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> const d ...
- Thread 线程池
Thread 线程池: 当使用多个较短存活期的线程有利时,运用线程池技术可以发挥作用.运用这一技术时,不是为每个任务创建一个全新的线程,而可以从线程池中抽出线程,并分配给任务.当线程完成任务后,再把它 ...