目录
一、读写锁(Read-Write Lock)
1. 基本概念
读写锁是一种允许多个线程 同时读取共享资源,但 写入时独占资源 的锁机制。它分为两种模式:
- 读模式(共享锁):允许多个线程同时持有。
- 写模式(独占锁):仅允许一个线程持有,且与其他读/写锁互斥。
适用场景:
- 读多写少 的场景(如缓存系统、数据库索引)。
- 读操作频繁且无需修改数据,而写操作较少但需确保一致性。
2. 核心函数(POSIX)
#include <pthread.h>
int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);
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. 示例:读写锁的使用
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_rwlock_t rwlock;
int data = 0;
void* reader(void* arg) {
for (int i = 0; i < 5; i++) {
pthread_rwlock_rdlock(&rwlock);
printf("Reader: data = %d\n", data);
pthread_rwlock_unlock(&rwlock);
usleep(100000); // 模拟读操作耗时
}
return NULL;
}
void* writer(void* arg) {
for (int i = 0; i < 3; i++) {
pthread_rwlock_wrlock(&rwlock);
data++;
printf("Writer: data updated to %d\n", data);
pthread_rwlock_unlock(&rwlock);
usleep(200000); // 模拟写操作耗时
}
return NULL;
}
int main() {
pthread_t r1, r2, w1;
// 初始化读写锁
pthread_rwlock_init(&rwlock, NULL);
// 创建线程
pthread_create(&w1, NULL, writer, NULL);
pthread_create(&r1, NULL, reader, NULL);
pthread_create(&r2, NULL, reader, NULL);
// 等待线程结束
pthread_join(w1, NULL);
pthread_join(r1, NULL);
pthread_join(r2, NULL);
// 销毁读写锁
pthread_rwlock_destroy(&rwlock);
return 0;
}
4. 读写锁的特性
| 特性 | 描述 |
|---|---|
| 读读不互斥 | 多个读线程可同时访问共享资源。 |
| 读写互斥 | 写操作会阻塞所有读操作和写操作。 |
| 写写互斥 | 多个写线程不能同时执行写操作。 |
5. 注意事项
- 写操作的优先级问题:某些实现中,写操作可能优先于读操作(如避免写线程饥饿)。
- 资源竞争:在高并发写场景下,读线程可能因等待写锁而延迟。
- 内存屏障:需确保锁操作后的内存可见性(POSIX默认处理)。
二、自旋锁(Spinlock)
1. 基本概念
自旋锁是一种 忙等待(busy-wait) 的锁机制。当线程尝试获取锁时:
- 如果锁被占用,线程会持续循环检查锁状态,直到获得锁。
- 如果锁未被占用,线程直接获取锁并进入临界区。
适用场景:
- 锁持有时间极短 的场景(如 CPU 缓存同步)。
- 多核系统中,避免线程阻塞和上下文切换的开销。
2. 核心函数(POSIX)
#include <pthread.h>
int pthread_spin_init(pthread_spinlock_t *lock, int pshared);
int pthread_spin_destroy(pthread_spinlock_t *lock);
int pthread_spin_lock(pthread_spinlock_t *lock);
int pthread_spin_trylock(pthread_spinlock_t *lock);
int pthread_spin_unlock(pthread_spinlock_t *lock);
3. 示例:自旋锁的使用
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_spinlock_t spinlock;
int count = 0;
void* increment(void* arg) {
for (int i = 0; i < 100000; i++) {
pthread_spin_lock(&spinlock); // 自旋等待获取锁
count++;
pthread_spin_unlock(&spinlock); // 释放锁
}
return NULL;
}
int main() {
pthread_t t1, t2;
// 初始化自旋锁
pthread_spin_init(&spinlock, PTHREAD_PROCESS_PRIVATE);
// 创建线程
pthread_create(&t1, NULL, increment, NULL);
pthread_create(&t2, NULL, increment, NULL);
// 等待线程结束
pthread_join(t1, NULL);
pthread_join(t2, NULL);
// 销毁自旋锁
pthread_spin_destroy(&spinlock);
printf("Final count: %d\n", count); // 预期输出 200000
return 0;
}
4. 自旋锁的特性
| 特性 | 描述 |
|---|---|
| 低延迟 | 适合短时间持有锁的场景,避免线程阻塞。 |
| CPU资源消耗 | 在锁被占用时持续自旋,可能浪费CPU资源(尤其在单核系统)。 |
| 多核优化 | 在多核系统中,自旋等待的线程可在其他CPU核心上运行。 |
5. 注意事项
- 避免长时间持有自旋锁:可能导致CPU资源浪费。
- 活锁风险:多个线程自旋等待时,可能形成“死循环”。
- 优先级反转:高优先级线程可能因等待低优先级线程释放锁而延迟。
三、读写锁 vs 自旋锁
| 特性 | 读写锁 | 自旋锁 |
|---|---|---|
| 适用场景 | 读多写少的共享资源 | 锁持有时间极短的场景 |
| 并发性 | 读操作并发,写操作独占 | 仅允许一个线程持有锁 |
| 资源消耗 | 读操作轻量,写操作可能阻塞 | 自旋等待消耗CPU资源 |
| 实现复杂度 | 较复杂(需管理读写状态) | 简单(仅需标志位) |
| 典型应用 | 缓存系统、数据库索引 | 内核级同步、CPU缓存同步 |
四、常见问题与解决方案
1. 自旋锁的CPU资源浪费
- 原因:线程在锁被占用时持续自旋,导致CPU空转。
- 解决方案:
- 限制自旋次数(如设置最大自旋轮询次数)。
- 在单核系统中避免使用自旋锁。
2. 读写锁的写操作饥饿
- 原因:读线程持续获取读锁,导致写线程无法获取锁。
- 解决方案:
- 使用公平锁(Fair Lock)策略,优先唤醒等待的写线程。
- 控制读线程的读取频率,避免长期占用读锁。
3. 锁的错误使用导致死锁
- 原因:
- 线程在持有锁时等待另一个锁。
- 自旋锁未释放或读写锁未解锁。
- 解决方案:
- 确保所有锁操作在
try...finally或RAII模式中释放。 - 避免嵌套加锁,或按固定顺序加锁。
- 确保所有锁操作在
五、性能优化建议
| 锁类型 | 优化策略 |
|---|---|
| 读写锁 | - 优先使用读锁,减少写锁的使用频率。<br>- 对于频繁更新的资源,考虑使用无锁数据结构。 |
| 自旋锁 | - 仅用于短时间临界区。<br>- 在多核系统中,自旋锁性能更优。 |
| 通用策略 | - 避免锁的粒度过大,减少锁的持有时间。<br>- 使用线程池管理线程生命周期。 |
六、总结
- 读写锁 适用于 读多写少 的场景,通过共享读锁提升并发性。
- 自旋锁 适用于 短时间持有锁 的场景,避免线程阻塞的开销。
- 选择锁机制 时需权衡并发性、资源消耗和实现复杂度。
- 错误处理:始终检查锁操作的返回值,确保资源正确释放。

1040

被折叠的 条评论
为什么被折叠?



