线程同步机制

互斥锁通信机制

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的更多相关文章

  1. linux高级管理第十二章--rsync

    实验部分 1.安装rsync 2.配置文件 3.配置密码 4.后续 5.为了测试,创建几个文件 配置实时同步 1.调整inotify内核参数 安装inotify-tools 测试同步 编写脚本 验证 ...

  2. 读书笔记 - js高级程序设计 - 第十二章 DOM2和DOM3

      Node类型的变化   访问元素的样式 myDiv.style.backgroundColor = "red" myDiv.style.width = "100px& ...

  3. 疯狂JAVA讲义---第十二章:Swing编程(五)进度条和滑动条

    http://blog.csdn.net/terryzero/article/details/3797782 疯狂JAVA讲义---第十二章:Swing编程(五)进度条和滑动条 标签: swing编程 ...

  4. 鸟哥的linux私房菜——第十二章学习(Shell Scripts)

    第十二章  Shell Scripts 1.0).什么是shell scripts? script 是"脚本.剧本"的意思.整句话是说, shell script 是针对 shel ...

  5. 读书笔记 - js高级程序设计 - 第十五章 使用Canvas绘图

    读书笔记 - js高级程序设计 - 第十三章 事件   canvas 具备绘图能力的2D上下文 及文本API 很多浏览器对WebGL的3D上下文支持还不够好   有时候即使浏览器支持,操作系统如果缺缺 ...

  6. 鸟哥的Linux私房菜——第十二章:档案的压缩与打包

    视频链接: 土豆:http://www.tudou.com/programs/view/GncwT0FJKsQ B站(推荐):http://www.bilibili.com/video/av98857 ...

  7. 第三十二章 Linux常规练习题(一)

    一.练习题一 1.超级用户(管理员用户)提示符是____,普通用户提示符是____.2.linux关机重启的命令有哪些 ?3.bash是什么?4.bash特性, 常见的bash特性有哪些?5.网卡的配 ...

  8. 第十二章 Linux三剑客之老三—grep

    一.Linux grep 命令用于查找文件里符合条件的字符串. Linux系统中的grep命令是一种功能强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来.grep全称是Global ...

  9. 【linux高级程序设计】(第九章)进程间通信-管道 1

    Linux操作系统所支持的主要进程间的通信机制. 无名管道 PIPE cat test.txt| grep hello 上面这种管道,将一个命令的输出作为另一个命令的输入,而这种管道是临时的,命令执行 ...

  10. 第十二章Linux文件系统与日志

    1.inode 包含文件的元信息(1)inode 内容:文件的字节数.拥有者的 UID.GID.文件的读写执行权限.时间戳等,但不包含文件名.文件名是储存在目录的目录项中.(2)查看文件的 inode ...

随机推荐

  1. PHP.TP框架下商品项目的优化3-php封装下拉框函数

    php封装下拉框函数 因为在项目中会经常使用到下拉框,所以根据一个表中的数据制作下拉框函数,以便调用 //使用一个表的数据做下拉框函数 function buildSelect($tableName, ...

  2. Android启动屏全屏显示

    1.为首页面设置一个theme <style name="app_start" > <item name="android:windowNoTitle& ...

  3. 《Cracking the Coding Interview》——第18章:难题——题目4

    2014-04-29 01:05 题目:数数从0到n总共有多少个数字‘2’? 解法:数位动态规划,可以O(log10(n))时间内解决. 代码: // 18.4 Count the number of ...

  4. windows下Tomcat安装

    环境Windows 64位 jdk1.8 1.Tomcat安装 官网地址:http://tomcat.apache.org/download-90.cgi 下载安装包,安装之后进行解压 2.修改htt ...

  5. webdriver--定位一组元素+iframe表单切换

    定位一组元素:find_elements,返回的是list,所以可以用列表的索引对列表里的某个元素操作,也可以用for循环访问list,依次操作各元素 driver.find_elements_by_ ...

  6. Windows7中如何让python2和python3共存并使用pip

    1.下载安装python2和python3 分别下载python2.7.exe.python3.6.exe并安装到C盘.E盘(如图)     2.配置环境变量 打开“系统变量”中的path文本框(如图 ...

  7. Android 图片文字单位 px、dp、sp区别

    文章来源:http://www.cnblogs.com/bjzhanghao/archive/2012/11/06/2757300.html px:像素,一个像素点,1px代表屏幕上一个物理的像素点: ...

  8. v-if与v-show区别

    在v-show中,元素是一直存在的,当v-show为false时,元素display:none只是隐藏了而已. v-if 作用:判断是否加载固定的内容,如果为真,则加载:为假时,则不加载. 用处:用在 ...

  9. Eclipse 日文乱码怎么解决Shift_JIS

    https://jingyan.baidu.com/article/870c6fc325a691b03fe4beac.html Eclipse设置编码的地方主要有三处,这三处的设置都会影响中文的显示. ...

  10. Python之Excel编程

    excel编程:excel中是unicode编码方式 需要使用xrld,xlwt和openpyxl这三个模块,需先通过pip install下载     xlrd 读取模块:xls,xlsx     ...