linux 互斥锁应用实例
更新时间:2023-05-25 18:13:01 阅读量: 实用文档 文档下载
- linux推荐度:
- 相关推荐
/*这是一个使用互斥锁实现两个线程之间同步实例,一个线程负责从标准输入设备中读取数据,而另一个线程则负责将读入的数据输出到标准输出设备上*/
#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 = 0;
int main(int argc,char *argv[])
{
int res;
pthread_t a_thread;
void *thread_result;
res = pthread_mutex_init(&work_mutex, NULL); //init mutex 初始化互斥锁
if (res != 0)
{
perror("Mutex initialization failed");
exit(EXIT_FAILURE);
}
res = pthread_create(&a_thread, NULL, thread_function, NULL);//create new thread创建新线程
if (res != 0)
{
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
pthread_mutex_lock(&work_mutex); //lock the mutex 互斥锁上锁
printf("Input some text. Enter 'end' to finish\n");
while(!time_to_exit) //标志
{
fgets(work_area, WORK_SIZE, stdin); //get a string from stdin读取一行信息 pthread_mutex_unlock(&work_mutex); //unlock the mutex解锁
while(1)
{
pthread_mutex_lock(&work_mutex); //lock the mutex
if (work_area[0] != '\0') //检查读入的内容输出没有
{
pthread_mutex_unlock(&work_mutex); //unlock the mutex如果没有输出,解锁
sleep(1);
}
else
{
break;
}
}
}
pthread_mutex_unlock(&work_mutex);//解锁
printf("\nWaiting for thread to finish...\n");
res = pthread_join(a_thread, &thread_result);//等待另一个线程结束
if (res != 0)
{
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined\n");
pthread_mutex_destroy(&work_mutex);//销毁互斥锁
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg)
{
sleep(1);
pthread_mutex_lock(&work_mutex);//上锁,抢占资源
while(strncmp("end", work_area, 3) != 0) //判断是不是结束信息end
{
printf("You input %d characters\n", strlen(work_area) -1);//时结束信心的话,就输出输入的字符个数
printf("the characters is %s",work_area);//输出输入的字符内容
work_area[0] = '\0';//设置最后以为为“\0”
pthread_mutex_unlock(&work_mutex);// 输出完成以后就解锁
sleep(1);
pthread_mutex_lock(&work_mutex);//上锁
while (work_area[0] == '\0' ) //判断第一位是不是为\0
{
pthread_mutex_unlock(&work_mutex);//解锁等待
sleep(1);
pthread_mutex_lock(&work_mutex);//上锁再次循环
}
}
time_to_exit = 1;//设置结束标志 work_area[0] = '\0';
pthread_mutex_unlock(&work_mutex); pthread_exit(0);
}
正在阅读:
linux 互斥锁应用实例05-25
劳动社保政策与管理实务复习要点11-22
2017年最新普速铁路接触网运行维修规则04-07
2014北京海淀中考一模化学11-25
以函数、数列、三角形、不等式等实际问题(教学案)-2017年高考数03-08
美容院坑人的这八个项目,千万别再做了!03-28
刊中报:职工年工作日调整为250天05-19
北京市丰台区2018届高三一模化学部分12-07
- 教学能力大赛决赛获奖-教学实施报告-(完整图文版)
- 互联网+数据中心行业分析报告
- 2017上海杨浦区高三一模数学试题及答案
- 招商部差旅接待管理制度(4-25)
- 学生游玩安全注意事项
- 学生信息管理系统(文档模板供参考)
- 叉车门架有限元分析及系统设计
- 2014帮助残疾人志愿者服务情况记录
- 叶绿体中色素的提取和分离实验
- 中国食物成分表2020年最新权威完整改进版
- 推动国土资源领域生态文明建设
- 给水管道冲洗和消毒记录
- 计算机软件专业自我评价
- 高中数学必修1-5知识点归纳
- 2018-2022年中国第五代移动通信技术(5G)产业深度分析及发展前景研究报告发展趋势(目录)
- 生产车间巡查制度
- 2018版中国光热发电行业深度研究报告目录
- (通用)2019年中考数学总复习 第一章 第四节 数的开方与二次根式课件
- 2017_2018学年高中语文第二单元第4课说数课件粤教版
- 上市新药Lumateperone(卢美哌隆)合成检索总结报告
- 互斥
- 实例
- 应用
- linux
- 岭东区第二小学2018-2019学年一年级下册数学期中测试卷
- S7-200PLC编程及应用题库大全
- 三叉神经瘤CT_MRI诊断同解剖对应分析
- 社会工作机构评估
- 各梯次详细课程表
- 2012年苏州市建设工程施工安全生产专项整治方案
- 给小学生家长的55条建议
- 七彩虹 战旗 C.A890GX X3 主板中文使用手册
- 二年级数学下册第二次月考精编试卷
- 北海市城市旅游导向系统专项规划项目说明
- 后备人才培养与人才梯队建设-人才梯队管理与考核.pdf
- 信息管理与系统开发设计综合模拟实习旭
- 七年级环境教育计划
- 会计学基础期末考试题重点及题目解答
- 望子成龙学校高二化学测试
- 2014安徽省会计基础最新考试试题库
- IFRS中文翻译版本
- 生物必修1 第三章 第四章 练习题及其答案
- 大学生社会实践报告两篇(家教类)
- 标准日本语初级二单元测试及答案