[Linux线程]线程的同步--使用互斥锁完成线程同步
#include<stdio.h>#include<stdlib.h>#include<pthread.h>pthread_mutex_tmutex;//定义一个互斥量intx;//定义一个全局变量//这是线程1的入口函数voidthreaddeal1(void){while(x>0)//如果X>0{pthread_mutex_lock(&mutex);//对互斥量进行加锁操作printf("线程1正在运行:x=%d\n",x);//输出当前的x值x--;//将x的值-1pthread_mutex_unlock(&mutex);//对互斥两进行开锁操作sleep(1);//休眠1秒}pthread_exit(NULL);//进程退出}//这是线程2的入口函数,线程2和线程1的操作完全相同voidthreaddeal2(void){while(x>0){pthread_mutex_lock(&mutex);printf("线程2正在运行:x=%d\n",x);x--;pthread_mutex_unlock(&mutex);sleep(1);}pthread_exit(NULL);}//这是主函数intmain(intargc,char*argv[]){pthread_tthreadid1,threadid2;intret;ret=pthread_mutex_init(&mutex,NULL);//初始化互斥锁if(ret!=0){printf("初始化互斥锁失败.\n");exit(1);}x=10;//给全局变量赋初始化值ret=pthread_create(&threadid1,NULL,(void*)&threaddeal1,NULL);//创建线程1if(ret!=0){printf("创建线程1失败.\n");exit(1);}ret=pthread_create(&threadid2,NULL,(void*)&threaddeal2,NULL);//创建线程2if(ret!=0){printf("创建线程2失败.\n");exit(1);}pthread_join(threadid1,NULL);pthread_join(threadid2,NULL);//阻塞线程1和线程2return(0);}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。