Linux应用 线程同步之互斥锁

1、概念

1.1 定义

互斥锁(Mutex),又称互斥型信号量,是一种特殊的二值性信号量,主要用于实现对共享资源的独占式处理。任意时刻,互斥锁的状态只有两种:开锁或闭锁。当有任务持有时,互斥锁处于闭锁状态,这个任务获得该互斥锁的所有权;当该任务释放它时,互斥锁被开锁,任务失去所有权。在一个任务持有互斥锁时,其他任务将不能再对该互斥锁进行开锁或持有。

1.2 应用场景

  • 保护共享资源:最常见的应用场景是保护共享资源,确保多个线程不会同时访问共享资源而导致数据竞争和不一致性。
  • 临界区保护:在多线程环境下,需要保护临界区(一段代码或数据结构)不被多个线程同时访问,互斥锁可以用来实现临界区的保护。
  • 线程同步:互斥锁也可以用于线程之间的同步,确保线程按照一定的顺序访问共享资源,避免竞争条件和死锁。

2、常用接口

2.1 pthread_mutex_init

初始化互斥锁对象。

int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
  • 入参:
    • mutex:指向互斥锁对象的指针。
    • attr:指向互斥锁属性对象的指针,通常传入NULL。
  • 返回值:若成功,返回0;否则返回错误码。

2.2 pthread_mutex_lock

加锁,尝试获取互斥锁。

int pthread_mutex_lock(pthread_mutex_t *mutex);
  • 入参:
    • mutex:指向互斥锁对象的指针。
  • 返回值:若成功,返回0;否则返回错误码。

2.3 pthread_mutex_unlock

解锁,释放互斥锁。

int pthread_mutex_unlock(pthread_mutex_t *mutex);
  • 入参:
    • mutex:指向互斥锁对象的指针。
  • 返回值:若成功,返回0;否则返回错误码。

2.4 pthread_mutex_destroy

销毁互斥锁对象。

  • 入参:
    • mutex:指向互斥锁对象的指针。
  • 返回值:若成功,返回0;否则返回错误码。

3、编程测试

示例中,定义了一个全局共享变量shared_resource,两个线程都会对这个变量进行增加操作。我们使用pthread_mutex_t类型的变量mutex作为互斥锁。在thread_function函数中,线程首先使用pthread_mutex_lock函数获取互斥锁,然后执行对共享资源的访问操作,最后使用pthread_mutex_unlock函数释放互斥锁。这样,当一个线程持有锁时,其他线程将无法获取锁,从而确保了同一时间只有一个线程能够访问共享资源。

main函数中,我们首先使用pthread_mutex_init函数初始化互斥锁,然后创建两个线程。我们使用pthread_create函数创建线程,并将thread_function作为线程函数。接着,我们使用pthread_join函数等待线程结束。最后,我们使用pthread_mutex_destroy函数销毁互斥锁,并输出共享资源的最终值。

当不使用互斥锁的时候代码如下:

#include <stdio.h>  
#include <stdlib.h>  
#include <pthread.h>  
  
// 全局共享变量  
int shared_resource = 0;  
  
// 互斥锁  
pthread_mutex_t mutex;  
  
// 线程函数  
void* thread_function(void* arg) 
{  
    // 锁定互斥锁  
    // pthread_mutex_lock(&mutex);  
  
    // 访问共享资源  
    for (int i = 0; i < 100000; i++) {  
        shared_resource++;  
    }  
  
    // 解锁互斥锁  
    // pthread_mutex_unlock(&mutex);  
  
    return NULL;  
}  
  
int main() {  
    // 初始化互斥锁  
    if (pthread_mutex_init(&mutex, NULL) != 0) 
    {  
        printf("Mutex initialization failed\n");  
        return 1;  
    }  
  
    // 创建两个线程  
    pthread_t thread1, thread2;  
    if (pthread_create(&thread1, NULL, thread_function, NULL) != 0) 
    {  
        printf("Thread 1 creation failed\n");  
        return 1;  
    }  
    if (pthread_create(&thread2, NULL, thread_function, NULL) != 0) 
    {  
        printf("Thread 2 creation failed\n");  
        return 1;  
    }  
  
    // 等待线程结束  
    pthread_join(thread1, NULL);  
    pthread_join(thread2, NULL);  
  
    // 销毁互斥锁  
    pthread_mutex_destroy(&mutex);  
  
    // 输出共享资源的最终值  
    printf("Shared resource value: %d\n", shared_resource);  
  
    return 0;  
}

测试发现两个线程访问同一个变量造成结果不可控:

放开代码中关于互斥锁的注释,测试代码如下:

#include <stdio.h>  
#include <stdlib.h>  
#include <pthread.h>  
  
// 全局共享变量  
int shared_resource = 0;  
  
// 互斥锁  
pthread_mutex_t mutex;  
  
// 线程函数  
void* thread_function(void* arg) 
{  
    // 锁定互斥锁  
    pthread_mutex_lock(&mutex);  
  
    // 访问共享资源  
    for (int i = 0; i < 100000; i++) {  
        shared_resource++;  
    }  
  
    // 解锁互斥锁  
    pthread_mutex_unlock(&mutex);  
  
    return NULL;  
}  
  
int main() {  
    // 初始化互斥锁  
    if (pthread_mutex_init(&mutex, NULL) != 0) 
    {  
        printf("Mutex initialization failed\n");  
        return 1;  
    }  
  
    // 创建两个线程  
    pthread_t thread1, thread2;  
    if (pthread_create(&thread1, NULL, thread_function, NULL) != 0) 
    {  
        printf("Thread 1 creation failed\n");  
        return 1;  
    }  
    if (pthread_create(&thread2, NULL, thread_function, NULL) != 0) 
    {  
        printf("Thread 2 creation failed\n");  
        return 1;  
    }  
  
    // 等待线程结束  
    pthread_join(thread1, NULL);  
    pthread_join(thread2, NULL);  
  
    // 销毁互斥锁  
    pthread_mutex_destroy(&mutex);  
  
    // 输出共享资源的最终值  
    printf("Shared resource value: %d\n", shared_resource);  
  
    return 0;  
}

测试结果如下,实现对变量的保护:

4、总结

本文讲解了linux线程同步中使用的互斥锁的概念的定义和应用场景,列举了常用的编程接口,并编写测试用例进行测试。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值