Linux系统编程之互斥量

思想:

每个线程在对资源操作前都尝试先加锁,成功加锁才能操作,操作结束解锁

互斥锁实质上是操作系统提供的一把“建议锁”(又称“协同锁”),建议程序中有多线程访问共享资源的时候使用该机制。但,并没有强制限定。

主要应用函数:

pthread_mutex_init函数
pthread_mutex_destroy函数
pthread_mutex_lock函数 :阻塞发等待
pthread_mutex_trylock函数
pthread_mutex_unlock函数

pthread_mutex_t 类型,其本质是一个结构体
pthread_mutex_t mutex; 变量mutex只有两种取值1、0

1、pthread_mutex_init函数

  • 函数原型:
    int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);

  • 参1:传出参数,调用时应传 &mutex

    restrict关键字:只用于限制指针,告诉编译器,所有修改该指针指向内存中内容的操作,只能通过本指针完成。不能通过除本指针以外的其他变量或指针修改

  • 参2:互斥量属性。是一个传入参数,通常传NULL,选用默认属性(线程间共享)

2、pthread_mutex_destroy函数

  • 销毁一个互斥锁 int
  • pthread_mutex_destroy(pthread_mutex_t *mutex)

3、pthread_mutex_lock函数

  • 加锁。可理解为将mutex–(或-1)
  • int pthread_mutex_lock(pthread_mutex_t *mutex);
    4、pthread_mutex_unlock函数
  • 解锁。可理解为将mutex ++(或+1)
  • int pthread_mutex_unlock(pthread_mutex_t *mutex);
    5、pthread_mutex_trylock函数
  • 尝试加锁
  • int pthread_mutex_trylock(pthread_mutex_t *mutex);

pthread_mutex_trylock函数和pthread_mutex_lock函数的区别:pthread_mutex_trylock不阻塞,pthread_mutex_lock阻塞

互斥量函数的应用:

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

void *tfn(void * arg)
{
 srand(time(NULL));
 while(1)
 {
    printf("hello ");
    sleep(rand()% 3);
    pritf("world\n");
    sleep(rand() % 3);
}
return NULL;
}


int  main()
{
 pthread_t tid;
 srand(time(NULL));
 pthread_create(&tid,NULL,tfn,NULL);
 while(1)
 {
     printf("HELLO");
    sleep(rand()% 3);
    pritf("WORLD\n");
    sleep(rand() % 3);
}
return 0;
}

输出:
在这里插入图片描述
输出出现数据混乱:
主控线程和子线程共同操作标准输出这一个共享资源,

修改:加锁

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
pthread_mutex_t mutex;
void *tfn(void * arg)
{
 srand(time(NULL));
 while(1)
 {
    pthread_mutex_lock(&mutex);
    printf("hello ");
    sleep(rand()% 3);
    printf("world\n");
        pthread_mutex_unlock(&mutex);
    sleep(rand() % 3);
}
return NULL;
}


int  main()
{
 pthread_t tid;
 srand(time(NULL));
 pthread_mutex_init(&mutex,NULL) //mutex == 1
 pthread_create(&tid,NULL,tfn,NULL);
 while(1)
 {
   pthread_mutex_lock(&mutex);
     printf("HELLO");
    sleep(rand()% 3);
    pritf("WORLD\n");
    pthread_mutex_unlock(&mutex);
    sleep(rand() % 3);
    
}
pthread_mutex_destory(&mutex);
return 0;
}

输出:
在这里插入图片描述
结论:

  • 在访问共享资源前加锁,访问结束后立即解锁。锁的“粒度”应越小越好。
  • 互斥锁仅供一个线程使用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在多线程编程中,为了防止多个线程同时访问共享资源而导致的数据竞争问题,需要使用同步机制来实现线程间的协调和互斥。而互斥是一种常用的同步机制,在多线程编程中被广泛使用。 互斥是一种线程同步原语,用于保护共享资源。当一个线程需要访问共享资源时,它需要先获取该资源的互斥。如果该互斥已经被其他线程占用,则当前线程会被阻塞,直到该互斥被释放。一旦该互斥被释放,当前线程就可以获取该互斥,访问共享资源,并将该互斥加锁。当该线程完成对共享资源的访问后,它需要将该互斥解锁,以便其他线程可以获取该互斥继续访问共享资源。 互斥的使用一般涉及到以下四个函数: 1. pthread_mutex_init():初始化互斥; 2. pthread_mutex_lock():加锁互斥; 3. pthread_mutex_unlock():解锁互斥; 4. pthread_mutex_destroy():销毁互斥。 下面是一个简单的例子,展示了如何使用互斥实现线程同步: ``` #include <stdio.h> #include <pthread.h> pthread_mutex_t mutex; // 定义互斥 void *thread_func(void *arg) { pthread_mutex_lock(&mutex); // 加锁互斥 printf("Thread %ld is running.\n", pthread_self()); pthread_mutex_unlock(&mutex); // 解锁互斥 pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t t1, t2; pthread_mutex_init(&mutex, NULL); // 初始化互斥 pthread_create(&t1, NULL, thread_func, NULL); pthread_create(&t2, NULL, thread_func, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); pthread_mutex_destroy(&mutex); // 销毁互斥 return 0; } ``` 在上面的例子中,我们定义了一个互斥 mutex,然后在线程函数中分别加锁和解锁该互斥。在主函数中,我们创建了两个线程,并等待它们执行完毕后退出程序。需要注意的是,我们必须在程序退出之前销毁该互斥,以免产生内存泄漏。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值