unix 线程同步之 条件变量 及 互斥锁 测试例子
发表于:2025-02-23 作者:千家信息网编辑
千家信息网最后更新 2025年02月23日,#include #include #include #include #include #include #include # define satisfy true# define u
千家信息网最后更新 2025年02月23日unix 线程同步之 条件变量 及 互斥锁 测试例子
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/time.h>
- #include <stdbool.h>
- #include <errno.h>
- #include <unistd.h>
- # define satisfy true
- # define unsatisfy false
- //pthread_mutex_t mut_loc = PTHREAD_MUTEX_INITIALIZER;//所为静态初始化为PTHREAD_MUTEX_INITIALIZER为一个常量,在全局中进行赋值
- pthread_mutex_t mut_loc;
- pthread_cond_t thd_con;
- struct timespec tsp;
- bool condition = unsatisfy;
- void maketimeout(struct timespec *tsp, int add_sec)//设置超时函数,当前的时间再加上需要等待时候,因为pthread_cond_timedwait只认识当前时间格式
- {
- struct timeval now;
- gettimeofday(&now,NULL);//获取当前时间
- tsp->tv_sec = now.tv_sec;
- tsp->tv_nsec = now.tv_usec *1000;
- tsp->tv_sec += add_sec; //等待20秒
- }
- void thread0(void)
- {
- int ret;
- if(0 != pthread_mutex_lock(&mut_loc))//上锁
- {
- perror("pthread_mutex_lock_0\n");
- return;
- }
- if(condition == unsatisfy)
- {
- fputs("条件不满足,重新等待条件!!\n",stdout);
- maketimeout(&tsp, 2);//设置超时时间2秒
- //等待条件,并且解锁,线程转到等待队列中,如果条件满足信号收到,即会进行上锁;
- //线程是处于一种叫做无竞争方式,由于条件未满足情况下不会与其它线程竞争锁,只有条件满足后才会进行竞争
- ret = pthread_cond_timedwait(&thd_con,&mut_loc,&tsp);
- if(ETIMEDOUT == ret)
- {
- fputs("直到超时条件都不满足,重新等待条件!!\n",stdout);
- }
- else if(0 == ret)
- {
- fputs("线程0在等待时候获得条件满足!\n",stdout);
- }
- else
- {
- perror("other error for pthread_cond_timedwait \n");
- pthread_exit((void *)1);
- }
- if(condition == satisfy)
- {
- fputs("0_条件满足!!\n",stdout);
- condition = unsatisfy;
- }
- }
- else if(condition == satisfy)
- {
- fputs("1_条件满足!!\n",stdout);
- condition = unsatisfy;
- }
- else
- {
- perror("error condition\n ");
- pthread_exit((void *)1);
- }
- if(0 != pthread_mutex_unlock(&mut_loc))
- {
- perror("pthread_mutex_lock_0\n");
- return;
- }
- pthread_exit((void *)0);
- }
- void thread1(void)
- {
- int ret;
- ret = pthread_mutex_trylock(&mut_loc);
- if(EBUSY == ret)
- {
- fputs("锁被线程0所占有!\n",stdout);
- }
- else if(0 == ret)
- {
- if(0 != pthread_cond_signal(&thd_con))
- {
- perror("pthread_cond_signal\n");
- pthread_exit((void *)1);
- }
- condition = satisfy;
- fputs("线程1使条件满足\n",stdout);
- if(0 != pthread_mutex_unlock(&mut_loc))
- {
- perror("pthread_mutex_lock_1\n");
- pthread_exit((void *)1);
- }
- }
- else
- {
- perror("other errors for pthread_mutex_lock_1\n");
- pthread_exit((void *)1);
- }
- pthread_exit((void *)0);
- }
- int main(int argc, char* argv[])
- {
- pthread_t thd0, thd1;
- if(0 != pthread_mutex_init(&mut_loc,NULL))// pthread_mutex_init 与 pthread_mutex_destroy配对使用,因为其是动态即使用malloc来产生
- {
- perror("pthread_mutex_init\n");
- exit(1);
- }
- if(0 != pthread_cond_init(&thd_con,NULL))// pthread_cond_init 与 pthread_cond_destroy配对使用,因为其是动态即使用malloc来产生
- {
- perror("pthread_cond_init\n");
- exit(1);
- }
- if(0 != pthread_create(&thd0,NULL,(void*)thread0,NULL))//创建线程0
- {
- perror("pthread_create_0\n");
- exit(1);
- }
- sleep(1);//让线程0先执行
- if(0 != pthread_create(&thd1,NULL,(void*)thread1,NULL))//创建线程1
- {
- perror("pthread_create_0\n");
- exit(1);
- }
- if(0 != pthread_join(thd1,NULL))//如果线程牌分离属性此函数不可用,如果线程1不退出,则处于阻塞状态
- {
- perror("pthread_join_0\n");
- exit(1);
- }
- if(0 != pthread_join(thd0,NULL))//如果线程牌分离属性此函数不可用,如果线程0不退出,则处于阻塞状态
- {
- perror("pthread_join_1\n");
- exit(1);
- }
- if(0 != pthread_cond_destroy(&thd_con))//与pthread_cond_init配对使用
- {
- perror("pthread_cond_destory\n");
- exit(1);
- }
- if(0 != pthread_mutex_destroy(&mut_loc))//与pthread_mutex_init配对使用
- {
- perror("pthread_mutex_init\n");
- exit(1);
- }
- return 0;
- }
线程
条件
时间
函数
竞争
动态
属性
时候
状态
阻塞
信号
全局
只有
常量
情况
方式
格式
队列
静态
例子
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
东丽区信息网络技术答疑解惑
云服务和软件开发事业部
合川公安局网络安全保卫支队长
代理服务器 负载均衡
IQ数据库启动报82错误
省市人大网络安全法执法检查
南京真诚软件开发
黄山app软件开发公司
clo软件开发有限公司
一般服务器的主机可以经常重启么
智星云gpu服务器
泸州市网络安全办公室
三峡大学网络安全周
网络安全进绿盟怎么样
自建云笔记服务器
数据库挖掘技术有用吗
看门狗2 服务器
建立国家反洗钱数据库
管家婆云服务器续费
mysql数据库实战讲解
开展网络安全教育宣传信息
网站服务器搭建与管理图书
法院加强网络安全建设
地理位置 数据库设计
违反网络安全法37条
数据库目录设置
好的数据库电脑
浪潮在国内服务器占比
微信怎么添加服务器繁忙
网络安全测评资质怎么办