<linux线程>同步互斥机制之【互斥锁】

本文基于信号量

如果信号量的值最多为1,那实际上相当于一个共享资源在任意时刻最多只能有一个线程在访问,这样的逻辑称为“互斥”。这时,有一种更加方便和语义更加准确的工具来满足这种逻辑---互斥锁。几个线程同时去抢一个锁,谁抢到了锁就进门把房间锁上,访问数据,出来后之前解锁。

照这样的逻辑,互斥锁的操作无非就是:

1.定义互斥锁。-----pthread_mutex_t类

2.初始化锁。--------pthread_mutex_init()函数  int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);

3.加锁。--------------pthread_mutex_lock()函数  int pthread_mutex_lock(pthread_mutex_t *mutex)

4.解锁。--------------pthread_mutex_unlock()函数  int pthread_mutex_unlock(pthread_mutex_t *mutex)

5.销毁互斥锁。----pthread_mutex_destroy()函数 int pthread_mutex_destroy(pthread_mutex_t *mutex)


C代码示例:

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

/* 1 定义互斥锁变量 */
pthread_mutex_t m;

// 被访问的资源,全局变量
int a = 100;

void *routine(void *arg)
{
	while (1)
	{
		/* 3 在访问资源之前,加锁 */
		pthread_mutex_lock(&m);
		
		// 访问资源
		printf("副1线程获得锁,读到资源:a=%d,占用1秒\n", a);
		sleep(1);
		
		/* 4 访问完后,解锁 */
		pthread_mutex_unlock(&m);
	}
	
		// 退出线程
		pthread_exit(NULL);
}

void *routine1(void *arg)
{
	while (1)
	{
		/* 3 在访问资源之前,加锁 */
		pthread_mutex_lock(&m);
		
		// 访问资源
		printf("副2线程获得锁,读到资源:a=%d,占用2秒\n", a);
		sleep(2);
		
		/* 4 访问完后,解锁 */
		pthread_mutex_unlock(&m);
	}
	
		// 退出线程
		pthread_exit(NULL);
}

int main(int argc, char const *argv[])
{
	/* 2 初始化互斥锁 */
	pthread_mutex_init(&m, NULL);
	
	// 创建线程
	pthread_t tid, tid1;
	pthread_create(&tid, NULL, routine, NULL);
	pthread_create(&tid1, NULL, routine1, NULL);
	
	sleep(1);
	while (1)
	{
		/* 3 在访问资源之前,加锁 */
		pthread_mutex_lock(&m);
		
		// 访问资源
		printf("主线程抢到锁,访问到到资源:a=%d, 占用3秒。\n", a);
		sleep(3);
		
		/* 4 访问完后,解锁 */
		pthread_mutex_unlock(&m);
		
		// 解锁完后休息一下,不然近水楼台先得月,会一直被主线程抢到,而且主线程会有优先权
		sleep(1);
		printf("\033[42m我休息一下,你们先抢!!\033[0m\n");
	}
	
	// 阻塞等待子线程退出、
	pthread_join(tid, NULL);
	pthread_join(tid1, NULL);
	
	/* 5 销毁互斥锁 */
	pthread_mutex_destroy(&m);
	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值