一、读写锁
读写锁和互斥量相似,都是对共享空间执行加锁和解锁的过程。不过,读写锁比互斥量有更高的并行性。
读写锁有三种状态:读模式加锁、写模式加锁和不加锁。读写锁只允许同时有一个线程占有写模式的锁。
这就意味着读锁可以同时被多个线程拥有,读写锁特别适合读次数远大于写次数的场景。
1.1 创建并初始化一把锁
读写锁的数据类型为pthread_rwlock_t
,使用以下方式初始化和销毁:
#include <pthread.h>
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,
const pthread_rwlockattr_t *restrict attr);
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
1.2 加锁和解锁
#include <pthread.h>
// 加读锁
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
// 加写锁
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
// 解锁
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
pthread_rwlock_rdlock()
可以同时对一把锁使用,不会阻塞线程。
1.3 带超时的读写锁
#include <pthread.h>
#include <time.h>
int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rwlock,
const struct timespec *restrict abs_timeou
int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rwlock,
const struct timespec *restrict abs_timeout);
二、示例
#include <stdio.h>
#include <pthread.h>
#include "log.h"
static int n = 0;
pthread_rwlock_t rwlock;
// 线程函数
void *f(void *args) {
int idx = 0;
while (idx++ < 1000) {
pthread_rwlock_wrlock(&rwlock);
n++;
pthread_rwlock_unlock(&rwlock);
}
return 0;
}
int main() {
int i;
pthread_t ts[50];
pthread_rwlock_init(&rwlock, NULL);
for (i = 0; i < 50; i++) {
pthread_create(&ts[i], 0, f, 0);
}
for (i = 0; i < 50; i++) {
pthread_join(ts[i], 0);
}
pthread_rwlock_destroy(&rwlock);
info("n = %d", n);
return 0;
}
执行结果:
此处评论已关闭