【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 ...
随机推荐
- Linux下添加桌面快捷方式
这里用Ubuntu下BurpSuite举例 sudo vim /home/user/Desktop/burpsuite.desktop //burpsuite随意起名,系统会系动创建文件 文件写入 # ...
- SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID
如题: SDK location not found. Define location with sdk.dir in the local.properties file or with an AND ...
- 安装完最小化 RHEL/CentOS 7 后需要做的 30 件事情(一)
本文导航 -1. 注册并启用红帽订阅 -2. 使用静态 IP 地址配置网络 -3. 设置服务器的主机名称 -4. 更新或升级最小化安装的 CentOS -5. 安装命令行 Web 浏览器 -6. 安装 ...
- 【vim环境配置】详细实录
[写在前面] 以下的所有内容主要参照: https://github.com/yangyangwithgnu/use_vim_as_ide . 原blog作者写的非常用心,建议大家都去看看.(个人觉得 ...
- ASP.NET Core [1]:Hosting(笔记)
参考:http://www.cnblogs.com/RainingNight/p/hosting-in-asp-net-core.html
- 如何将自己写的代码上传到github上
忙活了一下午终于成功把代码上传到github上. 接下来就是我上传代码的步骤: ①首先注册github账号,登录,创建新仓库 ,点击+,点击new repository 得到如下页面,填写仓库名,自己 ...
- 洛谷P2678跳石头(提高)
题目背景 一年一度的“跳石头”比赛又要开始了! 题目描述 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点. 在起点和终点之间,有 N 块岩石( ...
- c++知识点总结--静态与动态联编
静态联编是指在编译阶段就将函数实现和函数调用关联起来,因此静态联编也叫早绑定,在编译阶段就必须了解所有的函数或模块执行所需要检测的信息,它对函数的选择是基于指向对象的指针(或者引用)的类型 动态联 ...
- 转:sift算法详解
转自:http://blog.csdn.net/pi9nc/article/details/23302075 对于初学者,从David G.Lowe的论文到实现,有许多鸿沟,本文帮你跨越. 1.SIF ...
- URAL 1684. Jack's Last Word ( KMP next函数应用 )
题意:问第二行的串能不能恰好分割成几个串,使得这几个串都是第一行串的前缀.如果是,输出No, 并输出这几个串,否则输出Yes. 这题是Special Judge,把两个串连接起来,中间用一个未出现过的 ...