linux 互斥锁

  • 原子操作
    原子操作:最小的执行单位,执行时不可能被其他任务或事件打断,由于其特性因此用汇编编写。主要 用于资源计数(如引用计数,其实现主要利用volatile变量直接从寄存器中读值,防止编译器优化)

  • 互斥锁主要在多线程编程中保护共享资源,确保对关键代码段的独占式访问。对共享资源的访问,要对互斥量进行加锁,如果互斥量已经上了锁,调用线程会阻塞,直到互斥量被解锁。在完成了对共享资源的访问后,要对互斥量进行解锁

  • linux中相关函数使用说明(注:c++中有封装好了的mutex类,在头文件中)

    
    #include <pthread.h>  
    
    int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr);  
    int pthread_mutex_destroy(pthread_mutex_t *mutex);  
    int pthread_mutex_lock(pthread_mutex_t *mutex);  
    int pthread_mutex_trylock(pthread_mutex_t *mutex);  
    int pthread_mutex_unlock(pthread_mutex_t *mutex); 
    //函数成功返回0,任何其他返回值都表示错误。

    初始化

    pthread_mutex_init:初始化互斥锁,mutexattr指定互斥锁属性,NULL表示默认属性。对于静态分配的互斥量, 可以把它设置为PTHREAD_MUTEX_INITIALIZER, 或者调用pthread_mutex_init。对于动态分配的互斥量, 在申请内存(malloc)之后, 通过pthread_mutex_init进行初始化, 并且在释放内存(free)前需要调用pthread_mutex_destroy

    销毁

    pthread_mutex_destroy:销毁互斥锁,释放占用的内核资源。销毁一个已经加锁的互斥锁将导致不可预期的后果

    加锁

    pthread_mutex_lock:以原子操作给一个互斥锁加锁。如果目标互斥锁已经被加锁pthread_mutex_lock则被阻塞,直到该互斥锁占有者把它解锁
    pthread_mutex_trylock:和pthread_mutex_lock类似,不过它始终立即返回,而不论被操作的互斥锁是否加锁,是pthread_mutex_lock的非阻塞版本。当目标互斥锁未被加锁时,pthread_mutex_trylock进行加锁操作;如果互斥量被锁住了, trylock函数将不会阻塞等待而直接返回EBUSY, 表示共享资源处于忙状态。

    注意:这里讨论的pthread_mutex_lock和pthread_mutex_trylock是针对普通锁而言的,对于其他类型的锁,这两个加锁函数会有不同的行为

    死锁

    死锁主要发生在有多个依赖锁存在时,会在一个线程试图以与另一个线程相反顺序锁住互斥量时发生,如何避免死锁是使用互斥量应该格外注意的东西。

    解锁

    pthread_mutex_unlock以原子操作方式给一个互斥锁进行解锁操作。如果此时有其他线程正在等待这个互斥锁,则这些线程中的一个将获得它

  • 操作注意

    • 对共享资源操作前一定要获得锁
    • 完成操作以后一定要释放锁
    • 尽量短时间地占用锁
    • 如果有多锁, 如获得顺序是ABC连环扣, 释放顺序也应该是ABC
    • 线程错误返回时应该释放它所获得的锁
  • 互斥锁示例

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

#define err_sys(msg) \
    do { perror(msg); exit(-1); } while(0)
#define err_exit(msg) \
    do { fprintf(stderr, msg); exit(-1); } while(0)

int glab = 1;

void *r1(void *arg)
{
    pthread_mutex_t* mutex = (pthread_mutex_t *)arg;
    static int cnt = 10;

    while(cnt--)
    {
        pthread_mutex_lock(mutex);
        glab++;
        printf("I am in r1. cnt = %d\n", glab);
        pthread_mutex_unlock(mutex);
        sleep(1);
    }
    return "r1 over";
}

void *r2(void *arg)
{
    pthread_mutex_t* mutex = (pthread_mutex_t *)arg;
    static int cnt = 10;

    while(cnt--)
    {
        pthread_mutex_lock(mutex);
        glab++;
        printf("I am in r2. cnt = %d\n", glab);
        pthread_mutex_unlock(mutex);
        sleep(1);
    }
    return "r2 over";
}

int main(void)
{
    pthread_mutex_t mutex;
    pthread_t t1, t2;
    char* p1 = NULL;
    char* p2 = NULL;

    if(pthread_mutex_init(&mutex, NULL) < 0)
        err_sys("sem_init error");
    pthread_create(&t1, NULL, r1, &mutex);
    pthread_create(&t2, NULL, r2, &mutex);

    pthread_join(t1, (void **)&p1);//等待一个线程的结束,线程间同步的操作
    pthread_join(t2, (void **)&p2);
    pthread_mutex_destroy(&mutex);
    printf("s1: %s\n", p1);
    printf("s2: %s\n", p2);

    return 0;
}
  • 互斥锁属性
#include <pthread.h>
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
int pthread_mutexattr_init(pthread_mutexattr_t *attr);
int pthread_mutexattr_getpshared(const pthread_mutexattr_t *restrict attr, int *restrict pshared);
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);
int pthread_mutexattr_gettype(const pthread_mutexattr_t *restrict attr, int *restrict type);
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
  • 互斥锁常用两种属性:pshared和type

    • pshared指定是否允许跨进程共享互斥锁
      • PTHREAD_PROCESS_SHARED 互斥锁可以被跨进程共享
      • PTHREAD_PROCESS_PRIVATE 互斥锁只能被和锁的初始化线程隶属于同一个进程的线程共享
    • type指定互斥锁的类型
      • PTHREAD_MUTEX_NORMAL 普通锁,默认类型。当一个线程对一个普通锁加锁以后,其余请求该锁的线程将形成一个等待队列,并在该锁解锁后按优先级获得它。这种锁类型保证了资源分配的公平性。但这种锁也很容易引发问题:一个线程如果对一个已经加锁的普通锁再次加锁,将引发死锁;对一个已经被其他线程加锁的普通锁解锁,或者对一个已经解锁的普通锁解锁将导致不可预期的后果
      • PTHREAD_MUTEX_ERRORCHECK 检查锁。一个线程如果对一个已经加锁的检错锁再次加锁,则加锁操作返回EDEADLK。对一个已经被其让他线程加锁的检错锁解锁,或者对一个已经解锁的检错锁再次解锁,则检错锁返回EPERM。
      • PTHREAM_MUTEX_RECURSIVE 嵌套锁。这种锁允许一个线程在释放锁之前对他加锁而不发生死锁。不过其他线程如果要获得这个锁,则当前锁的拥有者必须执行相应次数的解锁操作。对一个已经被其他线程枷锁的嵌套锁解锁,或者对一个已经解锁的嵌套锁再次解锁,则解锁操作返回EPERM。
      • PTHREAD_MUTEX_DEFAULT 默认锁。一个线程如果对一个已经加锁的默认锁再次加锁,或者对一个已经被其他线程加锁的默认锁解锁,或者对一个已经解锁的默认锁再次解锁,将导致不可预期的后果。
  • 嵌套锁示例

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

int a = 1;
int b = 1;

pthread_mutex_t mutex;

void *pthread1(void *arg)
{
    while(1)
    {
        if(pthread_mutex_lock(&mutex) != 0)
        {
            printf("1 error\n");
            continue;
        }
        if(pthread_mutex_lock(&mutex) != 0) /* 此时调用了2次lock操作 */
        {
            printf("1 error\n");
            continue;
        }
        a++;
        b++;
        if(a != b)
            printf("pthread1: %d, %d\n", a, b);
        else
            printf("11\n");
        pthread_mutex_unlock(&mutex);
        pthread_mutex_unlock(&mutex);
        sleep(1);
    }
}

void *pthread2(void *arg)
{
    while(1)
    {
        if(pthread_mutex_lock(&mutex) != 0)
        {
            printf("2 error\n");
            continue;
        }
        a++;
        b++;
        if(a != b)
            printf("pthread2: %d, %d\n", a, b);
        else
            printf("22------------\n");
        pthread_mutex_unlock(&mutex);
        sleep(1);
    }
}

int main(void)
{
    pthread_t tid1, tid2;
    pthread_mutexattr_t attr;

    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
    pthread_mutex_init(&mutex, &attr);
    pthread_create(&tid1, NULL, pthread1, NULL);
    pthread_create(&tid2, NULL, pthread2, NULL);

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_mutex_destroy(&mutex);

    return 0;
}
//这里如果将PTHREAD_MUTEX_RECURSIVE改为其他属性,程序将不能正常运行

转载链接

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值