unix 线程同步之 条件变量 及 互斥锁 测试例子
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,#include #include #include #include #include #include #include # define satisfy true# define u
千家信息网最后更新 2026年11月01日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安全错误
数据库的锁怎样保障安全
云南咖啡
上海喔噻互联网科技有限公司
江苏根荣互联网科技
科技互联网龙头
科技创新与互联网经济
互联网科技的发展
广西互联网科技公司
齐家互联网科技有限公司
互联网金融监管科技
互联网科技公司的市场部门
互联网科技热点资讯
贵州叶脉互联网科技有限公司所属行业
福建科技产业互联网发展
乌鲁木齐新华智原互联网科技学校
佛山市翊博互联网络科技有限公司
2021互联网科技公司排名
互联网科技融合创新
南京一网一红互联网科技有限公司
深圳互联网科技园地址
互联网科技对化妆品行业
互联网的科技
互联网科技公司华为
北京互联网科技
电子科技大学互联网
广州慧亚互联网科技怎么样
栖莱科技互联网股份有限公司
辽宁互联网新科技
滨湖区互联网智能科技产品有哪些
江苏互联网智能科技产品出厂价
众联互联网络科技