linux线程互斥锁函数,[Linux线程]线程的同步--使用互斥锁完成线程同步

#include 

#include 

#include 

pthread_mutex_t mutex;    //定义一个互斥量

int x;                    //定义一个全局变量

//这是线程1的入口函数

void threaddeal1(void)

{

while(x>0)              //如果X>0

{

pthread_mutex_lock(&mutex);        //对互斥量进行加锁操作

printf("线程1正在运行: x=%d \n",x); //输出当前的x值

x--;                                //将x的值-1

pthread_mutex_unlock(&mutex);       //对互斥两进行开锁操作

sleep(1);                           //休眠1秒

}

pthread_exit(NULL);                   //进程退出

}

//这是线程2的入口函数,线程2和线程1的操作完全相同

void threaddeal2(void)

{

while(x>0)

{

pthread_mutex_lock(&mutex);

printf("线程2正在运行: x=%d \n",x);

x--;

pthread_mutex_unlock(&mutex);

sleep(1);

}

pthread_exit(NULL);

}

//这是主函数

int main(int argc,char *argv[])

{

pthread_t threadid1,threadid2;

int ret;

ret = pthread_mutex_init(&mutex,NULL);   //初始化互斥锁

if(ret != 0)

{

printf ("初始化互斥锁失败.\n");

exit (1);

}

x = 10;     //给全局变量赋初始化值

ret = pthread_create(&threadid1, NULL, (void *)&threaddeal1, NULL);  //创建线程1

if(ret != 0)

{

printf ("创建线程1失败.\n");

exit (1);

}

ret = pthread_create(&threadid2, NULL, (void *)&threaddeal2, NULL);  //创建线程2

if(ret != 0)

{

printf ("创建线程2失败.\n");

exit (1);

}

pthread_join(threadid1, NULL);

pthread_join(threadid2, NULL);    //阻塞线程1和线程2

return (0);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值