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;
    }
相关推荐
ACP广源盛139246256737 分钟前
(ACP广源盛)GSV6172---MIPI/LVDS 信号转换为 Type-C/DisplayPort 1.4/HDMI 2.0 并集成嵌入式 MCU
c语言·开发语言·单片机·嵌入式硬件·音视频
qq_401700411 小时前
I.MX6U 启动方式详解
linux
im_AMBER1 小时前
算法笔记 09
c语言·数据结构·c++·笔记·学习·算法·排序算法
code-vibe2 小时前
物理机 kali 改造笔记 (一)
linux·运维·服务器
老黄编程2 小时前
03-gpg(证书管理 )详细范例
linux·运维·ubuntu·数字证书
莱茶荼菜3 小时前
Ubuntu 20.04 系统库管理详细教程
linux·运维·ubuntu
是苏浙4 小时前
零基础入门C语言之C语言内存函数
c语言·开发语言
迅为电子4 小时前
嵌入式Linux新手入门:北京迅为3568开发板驱动开发第二章helloworld 驱动实验
linux·运维·驱动开发
闭着眼睛学算法6 小时前
【双机位A卷】华为OD笔试之【排序】双机位A-银行插队【Py/Java/C++/C/JS/Go六种语言】【欧弟算法】全网注释最详细分类最全的华子OD真题题解
java·c语言·javascript·c++·python·算法·华为od
nono牛7 小时前
MTK平台详解`adb devices`输出的序列号组成
android·linux·adb·智能手机