- #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;
- }
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/183636.html