linux互斥锁(pthread_mutex)知识点总结

对于多线程程序来说,我们往往需要对这些多线程进行同步。同步(synchronization)是指在一定的时间内只允许某一个线程访问某个资源。而在此时间内,不允许其它的线程访问该资源。我们可以通过互斥锁(mutex),条件变量(condition variable)和读写锁(reader-writer lock)来同步资源。

与互斥锁相关API

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

  在设计时需要规定所有的线程必须遵守相同的数据访问规则。只有这样,互斥机制才能正常工作。操作系统并不会做数据访问的串行化。如果允许其中的某个线程在没有得到锁的情况下也可以访问共享资源,那么即使其它的线程在使用共享资源前都获取了锁,也还是会出现数据不一致的问题。

  互斥变量用pthread_mutex_t数据类型表示。在使用互斥变量前必须对它进行初始化,可以把它置为常量PTHREAD_MUTEX_INITIALIZER(只对静态分配的互斥量),也可以通过调用pthread_mutex_init函数进行初始化。如果动态地分配互斥量(例如通过调用malloc函数),那么在释放内存前需要调用pthread_mutex_destroy。

1. 创建及销毁互斥锁

#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
// 返回:若成功返回0,否则返回错误编号

要用默认的属性初始化互斥量,只需把attr设置为NULL。  

2. 加锁及解锁

#include <pthread.h>
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_trylock尝试对互斥量进行加锁。如果调用pthread_mutex_trylock时互斥量处于未锁住状态,那么pthread_mutex_trylock将锁住互斥量,不会出现阻塞并返回0,否则pthread_mutex_trylock就会失败,不能锁住互斥量,而返回EBUSY。

示例代码:

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

//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
int g_data = 0;


pthread_mutex_t mutex;


void *func1(void *arg)
{
    int i;

    pthread_mutex_lock(&mutex);
    for(i=0;i<5;i++){
        printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
        printf("t1:param is %d\n",*((int *)arg));
        sleep(1);
    }
    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);

}

void *func3(void *arg)
{

    pthread_mutex_lock(&mutex);

    printf("t3:%ld thread is create\n",(unsigned long)pthread_self());
    printf("t3:param is %d\n",*((int *)arg));

    pthread_mutex_unlock(&mutex);

}

int main()
{
    int ret;
    int param = 100;
    pthread_t t1;
    pthread_t t2;
    pthread_t t3;

    pthread_mutex_init(&mutex, NULL);

    
    ret = pthread_create(&t1, NULL, func1,(void *)&param);
    if(ret == 0){
        printf("main:create t1 success\n");
    }

    ret = pthread_create(&t2, NULL, func2,(void *)&param);
    if(ret == 0){
        printf("main:create t2 success\n");
    }

    ret = pthread_create(&t3, NULL, func3,(void *)&param);
    printf("main:%ld\n",(unsigned long)pthread_self());

    pthread_join(t1,NULL);
    pthread_join(t2,NULL);

    pthread_mutex_destroy(&mutex);

    return 0;
}


编译结果:t1会被main函数打断,但是不会被t2,t3打断,t1运行后t2,t3才能运行

我们也用互斥锁限制共享资源的访问

例如:让g_data = 3时,在线程t1退出

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

//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
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================================\n");
//            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_init(&mutex,NULL);

    ret = pthread_create(&t1, NULL, func1,(void *)&param);
    if(ret == 0){
        printf("main:create t1 success\n");
    }

    ret = pthread_create(&t2, NULL, func2,(void *)&param);
    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,NULL);
    pthread_join(t2,NULL);
    pthread_mutex_destroy(&mutex);

    return 0;
}


编译结果:

可以写个shell脚本验证一下编译结果,先简单介绍一下shell脚本吧

Shell脚本

Shell 脚本(shell script),是一种为 shell 编写的脚本程序。shell script是一种解释型语言,必须由解释器来执行这些脚本,执行时,解释器将脚本一行一行地转换为代码。

这个解释器就是Shell,它是一个用 C 语言编写的程序。常见的Shell有Bourne Shell(/usr/bin/sh或/bin/sh)和Bourne Again Shell(/bin/bash),sh由Steve Bourne开发,是Unix 标准默认的shell,bash由Brian Fox和Chet Ramey共同开发完成,是Linux标准默认的shell。


在linux中可以使用vim来编写shell script,通常使用.sh作为扩展名,sh代表shell。下面是一个简单的shell脚本:

//运行三次

编写完shell脚本,在执行脚本前我们需要给脚本添加执行权限,命令如下:

接着运行:

编译结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热爱嵌入式的小佳同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值