线程同步之互斥量(初始化 销毁 加锁 解锁)以及死锁现象

互斥量未加锁demo

#include<stdio.h>
#include<pthread.h>
     //  int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
	//	int pthread_join(pthread_t thread, void **retval);
	//void pthread_exit(void *retval);
	//	int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int global_data1=1;

pthread_mutex_t mutex;

void *func1(void *arg)
{
	int i ;
	//pthread_mutex_lock(&mutex);
	static int ret=120; //to prevent when func1 quit the ret mabe disappear
	printf("the t1_id=%lu\n",pthread_self());
	printf("t1 data = %d\n",*((int *)arg));
	for(i =0;i<5;i++)
	{
		printf("%d\n",global_data1++);
	}
	//pthread_mutex_unlock(&mutex);
	pthread_exit((void*)&ret);
}

void *func2(void *arg)
{
	int i;
	//pthread_mutex_lock(&mutex);
	static int ret=99; //to prevent when func1 quit the ret mabe disappear
	printf("the t2_id=%lu\n",pthread_self());
	printf("t2 data = %d\n",*((int *)arg)+7);
	for(i =0;i<5;i++)
	{
		printf("%d\n",global_data1++);
	}
	//pthread_mutex_unlock(&mutex);
	pthread_exit((void*)&ret);
}

int main ()
{
	pthread_t t1;//create thread 1
	pthread_t t2;//create thread 2
	int data = 100;
	int *pretval1=NULL;//use for receive the ti return value
	int *pretval2=NULL;//~~~~t2 return value
	pthread_mutex_init(&mutex, NULL);
	int flag1 = pthread_create(&t1,NULL,func1,(void *)&data);
	int flag2 = pthread_create(&t2,NULL,func2,(void *)&data);
	if (flag1==0&&flag2==0)
	{
		printf("t1 and t2  is create succss\n");
		printf("main thead_id=%lu\n",pthread_self());
	}
	pthread_join(t1,(void **)&pretval1);//main stop wait t1 process working done.
	pthread_join(t2,(void **)&pretval2);//main stop wait t2 process working done.
	printf("func1 retval =%d\n",*pretval1);
	printf("func2 retval =%d\n",*pretval2);
	pthread_mutex_destroy(&mutex);
	return 0;
}

运行结果:
在这里插入图片描述
互斥量加锁

#include<stdio.h>
#include<pthread.h>
     //  int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
	//	int pthread_join(pthread_t thread, void **retval);
	//void pthread_exit(void *retval);
	//	int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int global_data1=1;

pthread_mutex_t mutex;

void *func1(void *arg)
{
	int i ;
	pthread_mutex_lock(&mutex);
	static int ret=120; //to prevent when func1 quit the ret mabe disappear
	printf("the t1_id=%lu\n",pthread_self());
	printf("t1 data = %d\n",*((int *)arg));
	for(i =0;i<5;i++)
	{
		printf("%d\n",global_data1++);
	}
	pthread_mutex_unlock(&mutex);
	pthread_exit((void*)&ret);
}

void *func2(void *arg)
{
	int i;
	pthread_mutex_lock(&mutex);
	static int ret=99; //to prevent when func1 quit the ret mabe disappear
	printf("the t2_id=%lu\n",pthread_self());
	printf("t2 data = %d\n",*((int *)arg)+7);
	for(i =0;i<5;i++)
	{
		printf("%d\n",global_data1++);
	}
	pthread_mutex_unlock(&mutex);
	pthread_exit((void*)&ret);
}

int main ()
{
	pthread_t t1;//create thread 1
	pthread_t t2;//create thread 2
	int data = 100;
	int *pretval1=NULL;//use for receive the ti return value
	int *pretval2=NULL;//~~~~t2 return value
	pthread_mutex_init(&mutex, NULL);
	int flag1 = pthread_create(&t1,NULL,func1,(void *)&data);
	int flag2 = pthread_create(&t2,NULL,func2,(void *)&data);
	if (flag1==0&&flag2==0)
	{
		printf("t1 and t2  is create succss\n");
		printf("main thead_id=%lu\n",pthread_self());
	}
	pthread_join(t1,(void **)&pretval1);//main stop wait t1 process working done.
	pthread_join(t2,(void **)&pretval2);//main stop wait t2 process working done.
	printf("func1 retval =%d\n",*pretval1);
	printf("func2 retval =%d\n",*pretval2);
	pthread_mutex_destroy(&mutex);
	return 0;
}

运行结果:
在这里插入图片描述
总结:当一个共享资源(全局变量)被加锁后,只允许当前线程使用,只有当使用完解锁以后,其他线程才可以调用。但这个锁谁先锁上无法保证。

死锁:

#include<stdio.h>
#include<pthread.h>
     //  int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
	//	int pthread_join(pthread_t thread, void **retval);
	//void pthread_exit(void *retval);
	//	int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int global_data1=1;

pthread_mutex_t mutex1;
pthread_mutex_t mutex2;

void *func1(void *arg)
{
	int i ;
	pthread_mutex_lock(&mutex1);
	sleep(1);
	pthread_mutex_lock(&mutex2);
	static int ret=120; //to prevent when func1 quit the ret mabe disappear
	printf("the t1_id=%lu\n",pthread_self());
	printf("t1 data = %d\n",*((int *)arg));
	for(i =0;i<5;i++)
	{
		printf("%d\n",global_data1++);
	}
	pthread_mutex_unlock(&mutex1);
	pthread_exit((void*)&ret);
}

void *func2(void *arg)
{
	int i;
	pthread_mutex_lock(&mutex2);
	sleep(1);
	pthread_mutex_lock(&mutex1);
	static int ret=99; //to prevent when func1 quit the ret mabe disappear
	printf("the t2_id=%lu\n",pthread_self());
	printf("t2 data = %d\n",*((int *)arg)+7);
	for(i =0;i<5;i++)
	{
		printf("%d\n",global_data1++);
	}
	pthread_mutex_unlock(&mutex2);
	pthread_exit((void*)&ret);
}

int main ()
{
	pthread_t t1;//create thread 1
	pthread_t t2;//create thread 2
	int data = 100;
	int *pretval1=NULL;//use for receive the ti return value
	int *pretval2=NULL;//~~~~t2 return value
	pthread_mutex_init(&mutex1, NULL);
	pthread_mutex_init(&mutex2, NULL);
	int flag1 = pthread_create(&t1,NULL,func1,(void *)&data);
	int flag2 = pthread_create(&t2,NULL,func2,(void *)&data);
	if (flag1==0&&flag2==0)
	{
		printf("t1 and t2  is create succss\n");
		printf("main thead_id=%lu\n",pthread_self());
	}
	pthread_join(t1,(void **)&pretval1);//main stop wait t1 process working done.
	pthread_join(t2,(void **)&pretval2);//main stop wait t2 process working done.
	printf("func1 retval =%d\n",*pretval1);
	printf("func2 retval =%d\n",*pretval2);
	pthread_mutex_destroy(&mutex1);
	pthread_mutex_destroy(&mutex2);

	return 0;
}

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值