Linux系统编程—线程—线程同步之互斥量加锁解锁

互斥量从本质上来说是一把锁,在访问共享资源前对互斥量进行加锁,在访问完成后释放互斥量上的锁。对互斥量进行加锁后,任何其他试图再次对互斥量加锁的线程将会被阻塞直到当前线程释放该互斥锁。如果释放互斥锁时有多个线程阻塞,所有在该互斥锁上的阻塞线程都会变成可运行状态,第一个变为可运行状态的线程可以对互斥量加锁,其他线程将会看到互斥锁依然被锁住,只能回去等待它重新变为可用。在这种方式下,每次只有一个线程可以向前运行。

互斥锁的特点:
1、原子性:把一个互斥量锁定为一个原子操作,这意味着操作系统(或pthread函数库)保证了如果一个线程锁定了一个互斥量,没有其他线程在同一时间可以成功锁定这个互斥量;
2、唯一性:如果一个线程锁定了一个互斥量,在它解除锁定之前,没有其他线程可以锁定这个互斥量;
3、非繁忙等待:如果一个线程已经锁定了一个互斥量,第二个线程又试图去锁定这个互斥量,则第二个线程将被挂起(不占用任何cpu资源),直到第一个线程解除对这个互斥量的锁定为止,第二个线程则被唤醒并继续执行,同时锁定这个互斥量。

操作系统并不会做数据访问的串行化。如果允许其中的某个线程在没有得到锁的情况下也可以访问共享资源,那么即使其它的线程在使用共享资源前都获取了锁,也还是会出现数据不一致的问题。
1、创建互斥锁

int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);

2、销毁互斥锁

int pthread_mutex_destroy(pthread_mutex_t*restrict mutex);// 返回:若成功返回0,否则返回错误编号

3、加锁

int pthread_mutex_lock(pthread_mutex_t *restrict mutex);

4、解锁

int pthread_mutex_unlock(pthread_mutex_t *restrict mutex);
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

pthread_mutex_t mutex;
int g_data=0;

void *func1(void *arg)
{
        int i;
        pthread_mutex_lock(&mutex);
        for(i=0;i<5;i++){
                printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
                printf("t1:param is %d\n",*((int *)arg));
                sleep(1);
        }
        pthread_mutex_unlock(&mutex);
}

void *func2(void *arg)
{
        pthread_mutex_lock(&mutex);

        printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
        printf("t2:param is %d\n",*((int *)arg));

        pthread_mutex_unlock(&mutex);
}

void *func3(void *arg)
{
        pthread_mutex_lock(&mutex);

        printf("t3:%ld thread is create\n",(unsigned long)pthread_self());
        printf("t3:param is %d\n",*((int *)arg));

        pthread_mutex_unlock(&mutex);
}

int main()
{
        int param=100;
        int ret;

        pthread_t t1;
        pthread_t t2;
        pthread_t t3;

        pthread_mutex_init(&mutex,NULL);
        ret=pthread_create(&t1,NULL,func1,(void *)&param);
        if(ret==0){
                printf("main:create t1 success\n" );
        }

        ret=pthread_create(&t2,NULL,func2,(void *)&param);
        if(ret==0){
                printf("main:create t2 success\n" );
        }

        ret=pthread_create(&t3,NULL,func3,(void *)&param);
        printf("main:%ld\n",(unsigned long)pthread_self());

        pthread_join(t1,NULL);
        pthread_join(t2,NULL);
        pthread_join(t3,NULL);
        pthread_mutex_destroy(&mutex);

        return 0;
}

对互斥锁进行加锁后,任何其他试图再次对互斥锁加锁的线程将会被阻塞,直到锁被释放。对互斥锁进行加锁后,任何其他试图再次对互斥锁加锁的线程将会被阻塞,直到锁被释放。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是光哥呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值