当前位置: 首页 > news >正文

【Linux线程同步专题】一、什么是线程同步、互斥量与死锁

在这里插入图片描述

欢迎关注博主 Mindtechnist 或加入【Linux C/C++/Python社区】一起探讨和分享Linux C/C++/Python/Shell编程、机器人技术、机器学习、机器视觉、嵌入式AI相关领域的知识和技术。


一、线程同步、互斥量与死锁

  • 什么是同步
  • 互斥量mutex
    • 1. 锁的初始化和销毁
    • 2. 给共享资源加锁
    • 3. 死锁


专栏传送门 :《Linux从小白到大神》 | 系统学习Linux开发、VIM/GCC/GDB/Make工具、Linux文件IO、进程管理、进程通信、多线程等,请关注专栏免费学习。


什么是同步

同步就是指同时起步,协调一致。不同的对象,对同步的理解方式也不尽相同。比如说,设备同步是指在两个设备之间规定一个共同的时间参考;数据库同步是指让两个或多个数据库内容保持一致,或者按照需要部分保持一致;文件同步是指让两个或多个文件夹中的文件保持一致。而在多线程中,同步是指协同、协助、互相配合,主要目的是协调步骤,按照预先定好的顺序依次运行。线程同步就是指,一个线程在发出某一功能调用时,在没有得到结果之前,该调用不返回,并且同时其它线程为保证数据一致性,不能调用该功能。如果没有同步机制,会产生“与时间有关的错误 time related”,为了避免这种数据混乱,线程之间必须要同步。通过同步就可以解决这种与时间有关的错误,避免数据混乱。实际上,线程之间、进程之间、信号之间都需要同步机制。也就是说,多个控制流共同操作一个共享资源的情况,都需要同步。数据混乱主要有三个原因:

  • 资源共享(共享资源可能会导致数据混乱,而独享资源则不会)。
  • 调度随机,调度随机就会导致在访问数据的时候出现竞争。
  • 线程间缺乏同步机制

这三点中,前两点都是无法更改的,因为资源共享是提高数据传递效率的最佳方法,而一旦资源共享,就势必会出现竞争,只要存在竞争,就可能会导致数据混乱。所以,解决方法只能从同步机制下手,在访问共享资源的时候使用互斥机制。

互斥量mutex

1. 锁的初始化和销毁

  • 头文件及函数原型
#include <pthread.h>

/*销毁一个锁*/
int pthread_mutex_destroy(pthread_mutex_t *mutex);
/*初始化一个锁*/
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
/*也可以初始化一个锁*/
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  • 函数描述

    The pthread_mutex_destroy() function shall destroy the mutex object referenced by mutex; the mutex object becomes, in effect, uninitialized. An implementation may cause pthread_mutex_destroy() to set the object referenced by mutex to an invalid value. A destroyed mutex object can be reinitialized using pthread_mutex_init(); the results of otherwise referencing the object after it has been destroyed are undefined.

  • 函数参数

    • mutex:互斥量(锁),restrict关键字表示,凡是被restrict关键字修饰的变量,该变量所代表的内存块只能由该变量去修改,比如说这里的mutex,mutex变量代表的内存中的数据只能通过mutex变量去修改,不能通过其它指针等方式去修改。
    • attr:互斥量的属性,可以直接设为NULL。
  • 函数返回值

    If successful, the pthread_mutex_destroy() and pthread_mutex_init() functions shall return zero; otherwise, an error number shall be returned to indicate the error.

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);
  • 函数描述

    • The mutex object referenced by mutex shall be locked by calling pthread_mutex_lock(). If the mutex is already locked, the calling thread shall block until the mutex becomes available. This operation shall return with the mutex object referenced by mutex in the locked state with the calling thread as its owner. 如果当前资源可以获得则加锁成功;如果当前资源已经被其它线程加锁,那么将阻塞等待。
    • The pthread_mutex_trylock() function shall be equivalent to pthread_mutex_lock(), except that if the mutex object referenced by mutex is currently locked (by any thread, including the current thread), the call shall return immediately. If the mutex type is PTHREAD_MUTEX_RECURSIVE and the mutex is currently owned by the calling thread, the mutex lock count shall be incremented by one and the pthread_mutex_trylock() function shall immediately return success.
    • The pthread_mutex_unlock() function shall release the mutex object referenced by mutex. The manner in which a mutex is released is dependent upon the mutex’s type attribute. If there are threads blocked on the mutex object referenced by mutex when pthread_mutex_unlock() is called, resulting in the mutex becoming available, the scheduling policy shall determine which thread shall acquire the mutex.
  • 函数参数

    mutex:pthread_mutex_init初始化得到的锁。

  • 函数返回值

    If successful, the pthread_mutex_lock() and pthread_mutex_unlock() functions shall return zero; otherwise, an error number shall be returned to indicate the error. The pthread_mutex_trylock() function shall return zero if a lock on the mutex object referenced by mutex is acquired. Otherwise, an error number is returned to indicate the error.

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

/*初始化一个锁*/
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void* th1(void* arg)
{
    while(1)
    {
        /*上锁*/
        pthread_mutex_lock(&mutex);
        /*进入临界区,都要打屏,会竞争*/
        printf("11111");
        sleep(rand()%3);
        printf("22222\n");
        /*释放锁*/
        pthread_mutex_unlock(&mutex); /*加锁一定要最小区域加锁,一旦离开共享资源区,立即释放锁*/
        sleep(rand()%3);
    }
}

void* th2(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex); /*若加锁失败,阻塞等待*/
        printf("aaaaa");
        sleep(rand()%3);
        printf("bbbbb\n");
        pthread_mutex_unlock(&mutex);
        sleep(rand()%3);
    }
}

int main(int argc, char* argv[])
{
    pthread_t tid[2];
    
    pthread_create(&tid[0], NULL, th1, NULL);
    pthread_create(&tid[1], NULL, th2, NULL);
    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);
    return 0;
}

3. 死锁

死锁主要有两种情况

  • 加锁之后,再一次加锁,那么第二个锁将会永久阻塞。
  • 交叉锁,同一个临界资源有两把锁k1和k2,此时线程1和线程2分别持有k1和k2导致永久阻塞;可以通过给锁的申请限制申请顺序来解决,或者已拥有一把锁,当另一把锁不可获取的时候,释放已持有的锁。

实际上,互斥量mutex只是建议锁,也就是说即使不加锁也能访问共享资源,并非操作系统强制只有加锁才能访问。


在这里插入图片描述
在这里插入图片描述


相关文章:

  • 内网渗透-Linux权限维持
  • Git 便捷操作
  • 美国项目管理协会和埃森哲最新报告:越来越多的公司设立首席转型官一职
  • y145.第八章 Servless和Knative从入门到精通 -- 消息系统基础和Eventing及实践(九)
  • CDQ整体二分-三维偏序(陌上花开)
  • Vue3+elementplus搭建通用管理系统实例十三:添加树形选择器及多选功能
  • GBASE 8s 高可用配置参数
  • 大白话paxos raft
  • 微信小程序开发入门与实战(插槽及组件页面的生命周期)
  • QT 语言的学习 day09 进程 和 线程
  • Golang-02Golang变量与基本数据类型
  • 在线五子棋对战 --- 人机对战的实现
  • 【微信小程序】shrio安全登录界面实现
  • Apache网页的优化,安全与防盗链
  • python中Try的运用及意义
  • 【mysql】环境安装、服务启动、密码设置
  • Linux中的硬链接与软链接
  • MQ框架的比较
  • Sass Day-01
  • unity如何实现一个固定宽度的orthagraphic相机
  • Yii源码解读-服务定位器(Service Locator)
  • 分布式熔断降级平台aegis
  • 个人博客开发系列:评论功能之GitHub账号OAuth授权
  • 关于Java中分层中遇到的一些问题
  • 技术胖1-4季视频复习— (看视频笔记)
  • 名企6年Java程序员的工作总结,写给在迷茫中的你!
  • 前端设计模式
  • 浅谈web中前端模板引擎的使用
  • 如何使用Mybatis第三方插件--PageHelper实现分页操作
  • 我的业余项目总结
  • 小程序开发中的那些坑
  • 一道闭包题引发的思考
  • #pragam once 和 #ifndef 预编译头
  • #大学#套接字
  • #图像处理
  • (c语言版)滑动窗口 给定一个字符串,只包含字母和数字,按要求找出字符串中的最长(连续)子串的长度
  • (WSI分类)WSI分类文献小综述 2024
  • (附源码)php新闻发布平台 毕业设计 141646
  • (附源码)spring boot网络空间安全实验教学示范中心网站 毕业设计 111454
  • (附源码)springboot 房产中介系统 毕业设计 312341
  • (论文阅读40-45)图像描述1
  • (万字长文)Spring的核心知识尽揽其中
  • (转) Face-Resources
  • .bat批处理(一):@echo off
  • .Net IE10 _doPostBack 未定义
  • .NET 跨平台图形库 SkiaSharp 基础应用
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地定义和使用弱事件
  • .net6使用Sejil可视化日志
  • :中兴通讯为何成功
  • @staticmethod和@classmethod的作用与区别
  • [2021ICPC济南 L] Strange Series (Bell 数 多项式exp)
  • [④ADRV902x]: Digital Filter Configuration(发射端)
  • [BZOJ2281][SDOI2011]黑白棋(K-Nim博弈)
  • [C++]类和对象【下】
  • [CF226E]Noble Knight's Path