unix线程同步之条件变量及互斥锁测试例子
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- # 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;
- }
在久治等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供做网站、成都网站制作 网站设计制作按需定制,公司网站建设,企业网站建设,品牌网站制作,全网整合营销推广,成都外贸网站建设,久治网站建设费用合理。
分享文章:unix线程同步之条件变量及互斥锁测试例子
URL网址:http://hbruida.cn/article/josppc.html