互斥锁
Linux中提供一把互斥锁mutex(也称之为互斥量)。每个线程在对资源操作前都尝试先加锁,成功加锁才能操作,操作结束解锁。
资源还是共享的,线程间也还是竞争的,但通过“锁”就将资源的访问变成互斥操作,而后与时间有关的错误也不会再产生了。
主要相关函数
-
pthread_mutex_t 类型
其本质是一个结构体,为简化理解,应用时可忽略其实现细节,简单当成整数看待。
pthread_mutex_t mutex; 变量mutex只有两种取值1、0。 -
pthread_mutex_init函数
函数描述:
初始化一个互斥锁(互斥量)---->初值可看作1
函数原型:
int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);
函数参数:
mutex:传出参数,调用时应传&mutex;
attr:互斥锁属性。是一个传入参数,通常传NULL,选用默认属性(线程间共享)。 -
pthread_mutex_destroy()函数
函数描述:
销毁一个锁;
函数原型:
int pthread_mutex_destroy(pthread_mutex_t *mutex);
函数参数:
mutex-- 互斥锁变量 -
pthread_mutex_lock()函数
函数描述:
对互斥锁加锁,可以理解为 mutex- -;
函数原型:
int pthread_mutex_lock(pthread_mutex_t* mutex);
函数参数:
mutex-- 互斥锁变量 -
pthread_mutex_unlock()函数
函数描述:
对互斥锁加锁,可以理解为 mutex++;
函数原型:
int pthread_mutex_unlock(pthread_mutex_t* mutex);
函数参数:
mutex-- 互斥锁变量 -
pthread_mutex_trylock()函数
函数描述:
尝试加锁
函数原型:
int pthread_mutex_trylock(pthread_mutex_t *mutex);
函数参数:
mutex-- 互斥锁变量
加锁和解锁
lock尝试加锁,如果加锁不成功,线程阻塞,阻塞到持有该互斥量的其他线程解锁为止。
unlock主动解锁函数,同时将阻塞在该锁上的所有线程全部唤醒,至于哪个线程先被唤醒,取决于优先级、调度。默认:先阻塞、先唤醒。
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<sys/types.h>
#Include<unistd.h>
#Include<string.h>
#include<time.h>
pthread_mutex_t mutex;
void *mythread1(void *arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
printf("hello ");
sleep(rand()%3);
printf("world\n");
pthread_mutex_unlock(&mutex);
sleep(rand()%3);
}
pthread_exit(NULL);
}
void *mythread2(void *arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
printf("HELLO ");
sleep(rand()%3);
printf("WORLD\n");
pthread_mutex_unlock(&mutex);
sleep(rand()%3);
}
pthread_exit(NULL);
}
int main()
{
pthread_t thread1;
pthread_t thread2;
pthread_mutex_init(&mutex);
srand(time(NULL));
int ret;
ret = pthread_create(&thread1,NULL,mythread1,NULL);
if(ret!=0)
{
printf("pthread_create error,[%s]\n",strerror(ret));
return -1;
}
ret = pthread_create(&thread2,NULL,mythread2,NULL);
if(ret!=0)
{
printf("pthread_create error,[%s]\n",strerror(ret));
return -1;
}
pthread_join(&theard1);
pthread_join(&theard2);
pthread_mutex_destroy(&mutex);
return 0;
}
读者可以将函数中加锁、解锁代码注释掉,运行对比没有注释掉的结果,体会加锁和不加锁的区别
读写锁
读写锁也叫共享-独占锁。当读写锁以读模式锁住时,它是以共享模式锁住的;当它以写模式锁住时,它是以独占模式锁住的。写独占、读共享。
读写锁非常适合于对数据结构读的次数远大于写的情况。
读写锁的特性
- 读写锁是“写模式加锁”时,解锁前,所有对该锁加锁的线程都会被阻塞。
- 读写锁是“读模式加锁”时,如果线程以读模式对其加锁会成功;如果线程以写模式加锁会阻塞。
- 读写锁是“读模式加锁”时, 既有试图以写模式加锁的线程,也有试图以读模式加锁的线程。那么读写锁会阻塞随后的读模式锁请求。优先满足写模式锁。读锁、写锁并行阻塞,写锁优先级高
场景练习
- 线程A加写锁成功, 线程B请求读锁
线程B阻塞 - 线程A持有读锁, 线程B请求写锁
线程B阻塞 - 线程A拥有读锁, 线程B请求读锁
线程B加锁成功 - 线程A持有读锁, 然后线程B请求写锁, 然后线程C请求读锁
线程B,C阻塞 --写的优先级高
线程A解锁,B线程加写锁成功,线程C继续阻塞
B解锁,C加读锁成功 - 线程A持有写锁, 然后线程B请求读锁, 然后线程C请求写锁
BC阻塞
A解锁,C加写锁成功,B继续阻塞
C解锁,B加读锁成功
读写锁主要操作函数
-
pthread_rwlock_t rwlock;定义一把读写锁
-
int pthread_rwlock_init()函数
函数描述:
初始化读写锁
函数原型:
int pthread_rwlock_init(
pthread_rwlock_t *restrict rwlock,
const pthread_rwlockattr_t *restrict attr);
函数参数:
rwlock-读写锁
attr-读写锁属性,传NULL为默认属性 -
销毁读写锁
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock); -
加读锁
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock); -
尝试加读锁
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock); -
加写锁
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock); -
尝试加写锁
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock); -
解锁
int pthread_rwlock_unlock(&pthread_rwlock_t *rwlock);
【例】3个线程不定时写同一全局资源,5个线程不定时读同一全局资源。
//读写锁测试程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
int number = 0;
//定义一把读写锁
pthread_rwlock_t rwlock;
//写线程回调函数
void *thread_write(void *arg)
{
int i = *(int *)arg;
int cur;
while(1)
{
//加写锁
pthread_rwlock_wrlock(&rwlock);
cur = number;
cur++;
number = cur;
printf("[%d]-W:[%d]\n", i, cur);
//解锁
pthread_rwlock_unlock(&rwlock);
sleep(rand()%3);
}
}
//读线程回调函数
void *thread_read(void *arg)
{
int i = *(int *)arg;
int cur;
while(1)
{
//加读锁
pthread_rwlock_rdlock(&rwlock);
cur = number;
printf("[%d]-R:[%d]\n", i, cur);
//解锁
pthread_rwlock_unlock(&rwlock);
sleep(rand()%3);
}
}
int main()
{
int n = 8;
int i = 0;
int arr[8];
pthread_t thread[8];
//读写锁初始化
pthread_rwlock_init(&rwlock, NULL);
//创建3个写子线程
for(i=0; i<3; i++)
{
arr[i] = i;
pthread_create(&thread[i], NULL, thread_write, &arr[i]);
}
//创建5个读子线程
for(i=3; i<n; i++)
{
arr[i] = i;
pthread_create(&thread[i], NULL, thread_read, &arr[i]);
}
//回收子线程
int j = 0;
for(j=0;j<n; j++)
{
pthread_join(thread[j], NULL);
}
//释放锁
pthread_rwlock_destroy(&rwlock);
return 0;
}