线程中的互斥量

一、互斥量的概念
互斥量从本质来说是一把锁,在访问共享资源前对互斥量进行设置(加锁),在访问完成后释放(解锁)互斥量。对互斥量进行加锁以后,任何其他试图再次对互斥量加锁的线程都会被阻塞,直到当前线程释放该互斥锁。如果解锁前有一个以上的线程阻塞,那么解锁后这些线程就变为运行状态,直到其中一个线程重新对互斥量加锁,这时其他线程又变为阻塞状态。

二、互斥量的作用
大部分情况,线程使用的数据都是局部变量,变量的地址空间在线程栈空间内,这种情况变量归属单个线程,其他线程无法获得变量。但有的时候,线程间需要共享很多变量,通过数据的共享,完成线程间的交互。如果多个线程并发地共享变量,会出现数据不一致的问题,而互斥量可以使共享资源被唯一访问,能很好地解决这个问题。
【例1】存在共享变量问题的售票系统代码

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

int ticket = 10;

void *route(void *arg)
{
    char *id = (char*)arg;
    while(1){
        if(ticket > 0){
            usleep(100000);
            printf("%s sells ticket:%d\n", id, ticket);
            ticket--;
        }else{
            break;
        }
    }
}

int main(void)
{
    pthread_t tid1, tid2, tid3, tid4;

    pthread_create(&tid1, NULL, route, "thread 1");
    pthread_create(&tid2, NULL, route, "thread 2");
    pthread_create(&tid3, NULL, route, "thread 3");
    pthread_create(&tid4, NULL, route, "thread 4");

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_join(tid3, NULL);
    pthread_join(tid4, NULL);
}

运行结果:
这里写图片描述
分析:为什么不能获得正确的结果?
(1)if语句判断条件为真以后,代码会并发地切换到其他线程;
(2)Usleep这个模拟漫长业务的过程,在这个漫长的业务过程中,可能有很多个线程会进入该代码段;
(3)ticket操作本身就不是一个原子操作。
证:取出ticket的部分汇编代码
这里写图片描述
可见:该操作并不是原子操作,而是对应多条指令
1)load:将共享变量ticket从内存加载到寄存器中;
2)Update:更新寄存器里面的值,执行-1操作;
3)Store:将新值从寄存器写到共享变量ticket的内存地址。
假设:剩下2张票,但是有4个人同时购买,就会出现运行结果中的-2张票,这样可以吗?显然是错误的!

要解决以上问题,需要做到三点:
1)代码必须要有互斥行为:当一个线程进入临界区执行时,不允许其他线程再进入该临界区。
2)如果多个线程同时要求执行临界区代码,并且临界区没有线程执行,那么只能允许一个线程进入临界区。
3)如果线程不在临界区中执行,那么该线程不能阻止其他线程进入临界区。
如下图所示:
这里写图片描述

三、互斥量的接口函数
1、初始化互斥量
初始化互斥量的两种方法:
(1)方法一:静态分配:
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER
(2)方法二:动态分配:
1)函数原型:Int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
2)参数:
mutex:要初始化的互斥量;
attr:NULL,表示默认属性;
3)返回值:成功返回0;否则返回错误编号。

2、销毁互斥量——
如果动态分配互斥量(例如,通过调用malloc函数),需要调用pthread_mutex_destroy函数释放内存。
(1)函数原型:int pthread_mutex_destroy(pthread_mutex_t *mutex);
(2)参数:mutex,表示要初始化的互斥量;
(3)返回值:成功返回0;否则返回错误编号。
(4)销毁互斥量需要注意:
A、使用PTHREAD_MUTEX_INITIALIZER初始化的互斥量不需要销毁;
B、不要销毁一个已经加锁的互斥量;
C、已经销毁的互斥量,要确保后面不会有线程在尝试加锁。

3、互斥量加锁和解锁
(1)函数原型:
1)int pthread_mutex_lock(pthread_mutex_t *mutex);
2)int pthread_mutex_trylock(pthread-mutex_t *mutex);
3)int pthread_mutex_unlock(pthread_mutex_t *mutex);
(2)参数:mutex,表示要初始化的互斥量;
(3)返回值:成功返回0,失败返回错误号
(4)释:加锁时,可能会遇到以下情况:
1)互斥量处于未锁状态,该函数会将互斥量锁定,同时返回成功。
2)发起函数调用时,其他线程已经锁定互斥量,或者存在其他线程同时申请互斥量,但是没有竞争到互斥量,那么pthread_lock调用会陷入阻塞,等待互斥量解锁。
3)如果线程不希望阻塞,它可以使用pthread_mutex_trylock尝试对互斥量进行加锁。
【例】改进上面的售票系统

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

int ticket = 10;
pthread_mutex_t mutex;

void *route(void *arg)
{
    char *id = (char*)arg;
    while(1){
        pthread_mutex_lock(&mutex);
        if(ticket > 0){
            usleep(1000);
            printf("%s sells ticket:%d\n", id, ticket);
            ticket--;
            pthread_mutex_unlock(&mutex);
            sched_yield();//需注意!
        }else{
            pthread_mutex_unlock(&mutex);
            break;
        }
    }
}

int main(void)

{
    pthread_t tid1, tid2, tid3, tid4;

    pthread_mutex_init(&mutex, NULL);

    pthread_create(&tid1, NULL, route, "thread 1");
    pthread_create(&tid2, NULL, route, "thread 2");
    pthread_create(&tid3, NULL, route, "thread 3");
    pthread_create(&tid4, NULL, route, "thread 4");

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_join(tid3, NULL);
    pthread_join(tid4, NULL);

    pthread_mutex_destroy(&mutex);
}

释:sched_yield()这个函数可以让等于或高于当前线程的线程先运行。如果没有符合条件的线程,那么这个函数将会立刻返回然后继续执行当前线程的程序。 在成功完成之后返回零,否则返回-1。
运行结果:
这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值