pthead 互斥锁使用详解

pthead 互斥锁使用

  • 互斥锁:一种简单的线程同步机制,它可以用来保护共享资源,防止多个线程同时修改共享资源而引发竞争条件。

pthread_mutex_init

  • 函数原型:

    c 复制代码
    int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
    • mutex:指向要初始化的互斥锁的指针。
    • attr:一个可选的指向互斥锁属性的指针,用于设置互斥锁的属性,可以为 NULL。
    • 返回值:成功返回 0,失败返回错误代码。
  • 用于初始化一个互斥锁。

  • 使用互斥锁时,应该先初始化再使用,最后再销毁。

pthread_mutex_destroy

  • 函数原型:

    c 复制代码
    int pthread_mutex_destroy(pthread_mutex_t *mutex);
    • mutex:指向要销毁的互斥锁的指针
    • 返回值:成功返回 0,失败返回错误代码。
  • 用于销毁一个互斥锁。

  • 销毁互斥锁之前,要确保没有任何线程正在持有该互斥锁,否则会导致未定义的结果。

pthread_mutex_lock

  • 函数原型:

    c 复制代码
    int pthread_mutex_lock(pthread_mutex_t *mutex);
    • mutex:要获取的互斥锁的指针。
  • 用于获取一个互斥锁。

  • 如果一个线程在获取互斥锁时发现该互斥锁已经被其他线程占用,那么它就会被阻塞,直到该互斥锁被释放为止。

  • 在使用互斥锁时,要确保在临界区内的代码尽可能少,以避免长时间占用互斥锁而导致的性能问题。

pthread_mutex_unlock

  • 函数原型:

    c 复制代码
    int pthread_mutex_unlock(pthread_mutex_t *mutex);
    • mutex:要释放的互斥锁的指针。
  • 用于释放一个互斥锁。

  • 只有成功获取互斥锁的线程才能使用 pthread_mutex_unlock 函数释放该互斥锁。如果一个线程在未获取互斥锁的情况下尝试调用 pthread_mutex_unlock 函数,将会产生未定义的行为。

  • 在释放互斥锁之后,其他等待获取该互斥锁的线程将有机会获取到互斥锁,并进入临界区执行其任务。

示例

  • 以下示例演示了两个线程交替写一个文件:

    c 复制代码
    #include <stdio.h>
    #include <string.h>
    #include <pthread.h>
    
    pthread_mutex_t g_mutex;
    
    void* thread1_func(void* arg)
    {
        FILE *fp = (FILE*)arg;
    
        // 获取互斥锁
        pthread_mutex_lock(&g_mutex);
    
        // 进入临界区,访问共享资源
        char *s = "thread1: hello\n";
        fwrite(s, strlen(s), 1, fp);
    
        // 释放互斥锁
        pthread_mutex_unlock(&g_mutex);
        return NULL;
    }
    
    void* thread2_func(void* arg)
    {
        FILE *fp = (FILE*)arg;
    
        // 获取互斥锁
        pthread_mutex_lock(&g_mutex);
    
        // 进入临界区,访问共享资源
        char *s = "thread2: hello\n";
        fwrite(s, strlen(s), 1, fp);
    
        // 释放互斥锁
        pthread_mutex_unlock(&g_mutex);
        return NULL;
    }
    
    int main()
    {
        // 新建文件
        FILE* fp = fopen("test.txt", "wt");
    
        // 初始化互斥锁
        pthread_mutex_init(&g_mutex, NULL);
    
        // 创建线程
        pthread_t th1;
        pthread_t th2;
        pthread_create(&th1, NULL, thread1_func, fp);
        pthread_create(&th2, NULL, thread2_func, fp);
    
        // 等待线程结束
        pthread_join(th1, NULL);
        pthread_join(th2, NULL);
    
        // 销毁互斥锁
        pthread_mutex_destroy(&g_mutex);
    
        // 关闭文件
        fclose(fp);
        return 0;
    }
相关推荐
ShineWinsu7 分钟前
对于Qt:TCP的解析
linux·c++·qt·网络协议·面试·tcp·客户端
瑶池酒剑仙40 分钟前
C++重构与代码质量完全指南:从设计模式、SOLID原则到代码审查与静态分析的工程实践(所赖君子见机)
c语言·开发语言·c++·设计模式·重构·哈希算法·visual studio
笔记本note1 小时前
Linux GPIO 技术详解
linux·gpio
东坡大表哥2 小时前
【Ubuntu】Ubuntu 20.04 LTS → 22.04 LTS 升级实录
android·linux·ubuntu
进击的荆棘4 小时前
Linux网络——应用层协议HTTP
linux·运维·服务器·网络·http
jipinxiaofeihua6 小时前
(文章7) 04章-C++异常处理(下)
linux·运维·网络
Go-higher6 小时前
C语言--数据结构-栈、队列、堆
c语言·数据结构
cpolar技术支持6 小时前
rsync 只看「大小 + 修改时间」:源文件改成 `CCCC-v3`、目标还是 `DDDD-v4`,三条同步命令 rc 全是 0
linux·rsync·cpolar·nas·数据同步
Rsingstarzengjx19 小时前
第三十九章 Linux PWM 驱动实验
linux·运维·服务器
SeanQhl20 小时前
BoostKit 环境准备
linux·openeuler·鲲鹏·boostkit·软件源