【linux高级程序设计】(第十二章)Linux多线程编程 2
线程同步机制
互斥锁通信机制
int pthread_mutex_init (pthread_mutex_t *__mutex, __const pthread_mutexattr_t *__mutexattr) :初始化互斥锁,成功返回0
参数1:要初始化的互斥锁
参数2:定义要初始化的互斥锁属性,NULL表默认
宏 PTHREAD_MUTEX_INITIALIZER 初始化静态分配互斥锁 ??这个语法是什么原理???
#define PTHREAD_MUTEX_INITIALIZER {{0,}}
初始化代码如下:
pthread_mutex_t mp = PTHREAD_MUTEX_INITIALIZER;
int pthread_mutex_destroy (pthread_mutex_t * __mutex) :销毁互斥锁,成功返回0
int pthread_mutex_lock (pthread_mutex_t *__mutex) :以阻塞方式申请互斥锁
int pthread_mutex_trylock (pthread_mutex_t *__mutex) :以非阻塞方式申请互斥锁
int pthread_mutex_unlock (pthread_mutex_t *__mutex) :释放互斥锁
例子:实现读写线程,读时不写,写时不读(我说不清楚下面的代码好不好,因为代码里面循环测试锁,跟我想象的不同,而且是写一句,读一句的。我觉得应该是可以任意写,读按顺序读)
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
#include<string.h>
void *thread_function (void *arg);
//全局互斥锁对象
pthread_mutex_t work_mutex;
#define WORK_SIZE 1024
//全局共享数据区
char work_area[WORK_SIZE];
int time_to_exit = ;
int main(int argc, char *argv[])
{
int res;
pthread_t a_thread;
void *thread_result;
//初始化互斥锁
res = pthread_mutex_init(&work_mutex, NULL);
if(res != )
{
printf("Mutex initialization failed");
exit(EXIT_FAILURE);
}
//创建新线程
res = pthread_create(&a_thread, NULL, thread_function, NULL);
if(res != )
{
printf("Thread creation failed");
exit(EXIT_FAILURE);
}
//接收输入前,给互斥锁上锁
pthread_mutex_lock(&work_mutex);
printf("Input some text. Enter 'end' to finish\n");
while(!time_to_exit)
{
fgets(work_area, WORK_SIZE, stdin);
//解锁
pthread_mutex_unlock(&work_mutex);
while()
{
//上锁
pthread_mutex_lock(&work_mutex);
if(work_area[] != '\0') //检测数据是否读出
{
pthread_mutex_unlock(&work_mutex); //如果没有输出,解锁
sleep();
}
else
{
break;
}
}
}
pthread_mutex_unlock(&work_mutex); //解锁
printf("\nWaiting for thread to finish...\n");
res = pthread_join(a_thread, &thread_result);
if(res != )
{
printf("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined\n");
pthread_mutex_destroy(&work_mutex);
exit(EXIT_SUCCESS);
} void *thread_function(void *arg)
{
sleep();
//上锁,抢占资源
pthread_mutex_lock(&work_mutex);
while(strncmp("end", work_area, ) != )
{
printf("You input %d characters\n", strlen(work_area));
printf("the characters is %s", work_area);
work_area[] = '\0';
pthread_mutex_unlock(&work_mutex);
sleep();
pthread_mutex_lock(&work_mutex);
while(work_area[] == '\0')
{
pthread_mutex_unlock(&work_mutex);
sleep();
pthread_mutex_lock(&work_mutex);
}
}
time_to_exit = ;
work_area[] = '\0';
pthread_mutex_unlock(&work_mutex);
pthread_exit();
}

【linux高级程序设计】(第十二章)Linux多线程编程 2的更多相关文章
- 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 ...
随机推荐
- JavaScript获取时间
var myDate = new Date(); console.log(myDate.getFullYear()); //获取完整的年份(4位,1970-????) ...
- Parameter 'limit' not found. Available parameters are [arg1, arg0, pa
mybatis代码报错,这是因为mapper识别不了limit,需要替换成 LIMIT #{arg0},#{arg1}
- android 管理Touch事件
The onInterceptTouchEvent() method gives a parent the chance to see any touch event before its child ...
- Delphi中的关键字与保留字
Delphi中的关键字与保留字 分类整理 Delphi 中的“关键字”和“保留字”,方便查询 感谢原作者的收集整理! 关键字和保留字的区别在于,关键字不推荐作标示符(编译器已经内置相关函数或者留给保留 ...
- 【Linear Support Vector Machine】林轩田机器学习技法
首先从介绍了Large_margin Separating Hyperplane的概念. (在linear separable的前提下)找到largest-margin的分界面,即最胖的那条分界线.下 ...
- Springboot 启动问题
每次以debug方式启动springboot之后都会在SilentExitExceptionHandler类中的throw new SilentExitException() 解决办法 :window ...
- JavaScript中最常用的55个经典技巧,没事的时候看看,拓展解决问题的思路
都转烂了,不过还是贴上来了.查的时候方便... test 1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键 & ...
- 聊聊、Java SPI
SPI,Service Provider Interface,服务提供者接口. Animal 接口 package com.rockcode.www.spi; public interface Ani ...
- UTXO是什么?
以易于理解的方式解释了比特币交易中的"UTXO" UTXO 2017年11月1日 让我们看看当你发一点硬币时会发生什么. 比特币交易通过UTXO执行.通过在比特硬币的所有交易中新生 ...
- Numpy 与 DataFrame对比与应用
(一)对比Numpty 与 DataFrame默认索引取值不同点 Numpy索引取值 #Numpy索引取值 data=np.empty((2,4),dtype=int) print(data) ''' ...