• Linux--互斥锁


    一、与互斥锁相关api

    **互斥量(mutex)**从本质上来说是一把锁。在访问共享资源前对互斥量进行加锁,在访问完成后释放互斥量。对互斥量进行枷锁后,任何其他试图再次对互斥量加锁的线程将会被阻塞直到当前线程释放该互斥锁。如果释放互斥锁时有多个线程阻塞,所有在该互斥锁上的阻塞线程都会变成可运行状态,第一个变成可运行状态的线程可以对互斥量加锁,其他线程将会看到互斥锁依然被锁住,只能回去等待它重新变为可用。在这种方式下,每次只有一个线程可以向前运行。
    1、创建及销毁互斥锁

    #include 
     
    int pthread_mutex_init(pthread_mutex_t * restrict mutex, const pthread_mutexattr_t *restrict attr);
    //初始化锁
    //指针,锁的属性,
    int pthread_mutex_destroy(pthread_mutex_t mutex);
    //销毁锁
    //返回:若成功返回0,否则返回错误编号
    
    /*互斥量用pthread_mutex_t数据类型表示。在使用互斥量前必须对它进行初始化,可以通过调用pthread_mutex_init函数进行初始化。如果动态地分配互斥量(例如通过调用malloc函数),那么在释放内存前需要调用 pthread_mutex_destroy.
    要用默认的属性初始化互斥量,只需要把attr设置为NULL。
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2、加锁及解锁:

    #include 
     
    int pthread_mutex_lock(pthread_mutex_t mutex);
    //上锁
    int pthread_mutex_trylock(pthread_mutex_t mutex);
    int pthread_mutex_unlock(pthread_mutex_t mutex);
    //解锁
    
    /* 如果线程不希望被阻塞,它可以使用pthread_mutex_trylock尝试对互斥量进行加锁。如果调用pthread_mutex_trylock时互斥量处于未锁住状态,那么pthread_mutex_trylock将锁住互斥量,不会出现阻塞并返回0,否则pthread_mutex_trylock就会失败,不能锁住互斥量,而返回EBUSY。
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3、例:

    //互斥锁
    
    
    #include 
    #include 
    
    int g_data = 0;
    
    pthread_mutex_t mutex;//创建锁
    
    void *func1(void *arg)
    {
    	pthread_mutex_lock(&mutex);//上锁
    	
    	printf("t1: %ld thread is create!\n",(unsigned long)pthread_self());
    	printf("t1: param is %d \n",*((int *)arg));
    
    	pthread_mutex_unlock(&mutex);//解锁
    }
    
    void *func2(void *arg)
    {
    	pthread_mutex_lock(&mutex);//上锁
    	
    	printf("t2: %ld thread is create!\n",(unsigned long)pthread_self());
    	printf("t2: param is %d \n",*((int *)arg));
    	
    	pthread_mutex_unlock(&mutex);//解锁
    }
    
    int main()
    {
    	int ret;
    	int param = 100;
    	pthread_t t1;
    	pthread_t t2; 
    	
    	pthread_mutex_init(&mutex,NULL);//初始化锁
    	
    	int *pret = NULL;
    	
    	//创建线程
    	ret = pthread_create(&t1,NULL,func1,(void *)&param);//调用func1函数
    	if(ret == 0){
    		printf("main: create t1 success! \n");
    	}
    	
    	ret = pthread_create(&t2,NULL,func2,(void *)&param);//调用func2函数
    	if(ret == 0){
    		printf("main: create t2 success! \n");
    	}
    	
    	printf("main: %ld\n",(unsigned long)pthread_self());
    	
    	//等待
    	pthread_join(t1,(void **)&pret);
    	pthread_join(t2,(void **)&pret);
    	
    	pthread_mutex_destroy(&mutex);//销毁锁
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    给t1 上锁后,t2不会运行,等t1完成后才会继续

    二、互斥锁限制共享资源的访问

    1、

    //互斥锁限制共享资源的访问
    
    
    #include 
    #include 
    
    int g_data = 0;
    
    pthread_mutex_t mutex;
    
    void *func1(void *arg)
    {
    	printf("t1: %ld thread is create!\n",(unsigned long)pthread_self());
    	printf("t1: param is %d \n",*((int *)arg));
    	
    	pthread_mutex_lock(&mutex);		//加锁
    	while(1){
    		printf("t1: %d\n",g_data++);
    		sleep(1);
    		
    		if(g_data == 3){
    			pthread_mutex_unlock(&mutex);		//删除锁
    			printf("-----------t1 quit !----------");
    			pthread_exit(NULL);
    			//exit(0); 退出整个进程
    		}
    	}
    	
    }
    
    void *func2(void *arg)
    {
    	printf("t2: %ld thread is create!\n",(unsigned long)pthread_self());
    	printf("t2: param is %d \n",*((int *)arg));
    	
    	
    	while(1){
    		printf("t2: %d\n",g_data);
    		pthread_mutex_lock(&mutex);
    		g_data++;
    		pthread_mutex_unlock(&mutex);
    		sleep(1);
    	}
    }
    
    int main()
    {
    	int ret;
    	int param = 100;
    	pthread_t t1;
    	pthread_t t2;
    	
    	pthread_mutex_t init(&mutex,NULL);
    	
    	
    	int *pret = NULL;
    	
    	//创建线程
    	ret = pthread_create(&t1,NULL,func1,(void *)&param);//调用func1函数
    	if(ret == 0){
    		printf("main: create t1 success! \n");
    	}
    	
    	ret = pthread_create(&t2,NULL,func1,(void *)&param);//调用func1函数
    	if(ret == 0){
    		printf("main: create t2 success! \n");
    	}
    	
    	printf("main: %ld\n",(unsigned long)pthread_self());
    	
    	while(1){
    		printf("main: %d\n",g_data);
    		sleep(1);
    	}
    	
    	//等待
    	pthread_join(t1,(void **)&pret);
    	pthread_join(t2,(void **)&pret);
    	pthread_mutex_destroy (&mutex);
    	
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

    结束进程:

    这里是引用
    ps -aux |grep a.out //查看pid
    kill pid //杀死进程

    2、死锁
    在加了两个锁的前提下,当线程A获得一把锁时,想要获得另外一把锁,线程B想要拿到对方手里的锁时,都不可以向下解锁,就会造成死锁。

    资源参考地址: https://www.cnblogs.com/xiehongfeng100/p/4620852.html

  • 相关阅读:
    C++ Reference: Standard C++ Library reference: C Library: cwchar: wcstold
    webpack 中,filename 和 chunkFilename 的区别
    Stages—研发过程可视化建模和管理平台
    生产者消费者模型
    想知道如何图片转文字?这几个方法你别错过
    【精读Uboot】异常向量的设置
    Kafka集群环境的部署
    红路灯识别
    C++文件服务器项目—FastDFS—1
    Java学习笔记 --- 多线程
  • 原文地址:https://blog.csdn.net/weixin_48208102/article/details/133019755