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;
    }
相关推荐
Codeking__13 分钟前
Linux——信号(1)信号的产生
linux·运维·服务器
Ares-Wang16 分钟前
Linux》》bash 、sh 执行脚本
linux·运维·bash
庐阳寒月23 分钟前
linux多线(进)程编程——(8)多进程的冲突问题
linux·c语言·嵌入式
_朱志强1 小时前
解决前端vue项目在linux上,npm install,node-sass 安装失败的问题
linux·前端·vue.js
结衣结衣.1 小时前
【MySQL】库的操作
linux·数据库·mysql
西木九1 小时前
WSL2-Ubuntu22.04安装URSim5.21.3
linux·ubuntu22.04·wsl2·ur机械臂·ur10e
野生派蒙2 小时前
Linux:显示 -bash-4.2$ 问题(CentOS 7)
linux·运维·服务器·centos·bash
槐月杰5 小时前
C语言中冒泡排序和快速排序的区别
c语言·算法·排序算法
清风~徐~来7 小时前
【Linux】环境变量
linux·运维·chrome
Willliam_william10 小时前
QEMU学习之路(8)— ARM32通过u-boot 启动Linux
linux·学习·elasticsearch