读者写者问题与读写锁自旋锁
一、读者写者问题
读者写者问题具有以下特点:
- 一个交易场所---写者写入数据,读者读数据
- 两种角色---读者,写者
- 三种关系
- 读者和读者---并发
- 写者和写者---互斥
- 读者和写者---互斥 && 同步
二、读者写者VS生产消费
生产者消费者模型中的消费者会将数据取走,而读者不会,这也是为什么读者之间不需要互斥,而是并发执行。
三、读者写者问题如何理解
通过下面伪代码来展示读者写者问题:
公共部分:
C++
uint32_t reader_count = 0; //读者数量
lock_t count_lock; //reader_count是临界资源,访问需要加锁
lock_t writer_lock; //写者之间 && 写者和读者之间 是 互斥的
Reader:
C++
// 加锁
lock(count_lock);
if(reader_count == 0)
lock(writer_lock);
++reader_count;
unlock(count_lock);
// 执行读操作
//解锁
lock(count_lock);
--reader_count;
if(reader_count == 0)
unlock(writer_lock);
unlock(count_lock);
Writer:
C++
lock(writer_lock);
// 执行写操作
unlock(writer_lock);
四、读写锁
在编写多线程的时候,有一种情况是十分常见的。那就是,有些公共数据修改的机会比较少。相比较改写,它们读的机会反而高的多。通常而言,在读的过程中,往往伴随着查找的操作,中间耗时很长。给这种代码段加锁,会极大地降低我们程序的效 率。那么有没有一种方法,可以专门处理这种多读少写的情况呢?
有,那就是读写锁。
注意:写独占,读共享,读锁优先级高。
读写锁接口:
设置读写优先
C
int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, int pref);
/* pref 共有 3 种选择
PTHREAD_RWLOCK_PREFER_READER_NP (默认设置) 读者优先,可能会导致写者饥饿情况
PTHREAD_RWLOCK_PREFER_WRITER_NP 写者优先,目前有 BUG,导致表现行为和
PTHREAD_RWLOCK_PREFER_READER_NP 一致
PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP 写者优先,但写者不能递 归加锁
*/
初始化
C
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,const pthread_rwlockattr_t *restrict attr);
销毁
C
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
加锁和解锁
C
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
示例代码:
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <vector>
#include <cstdlib>
#include <ctime>// 共享资源
int shared_data = 0;// 读写锁
pthread_rwlock_t rwlock;// 读者线程函数
void *Reader(void *arg)
{//sleep(1); //读者优先,一旦读者进入&&读者很多,写者基本就很难进入了int number = *(int *)arg;while (true){pthread_rwlock_rdlock(&rwlock); // 读者加锁std::cout << "读者-" << number << " 正在读取数据, 数据是: " << shared_data << std::endl;sleep(1); // 模拟读取操作pthread_rwlock_unlock(&rwlock); // 解锁}delete (int*)arg;return NULL;
}// 写者线程函数
void *Writer(void *arg)
{int number = *(int *)arg;while (true){pthread_rwlock_wrlock(&rwlock); // 写者加锁shared_data = rand() % 100; // 修改共享数据std::cout << "写者- " << number << " 正在写入. 新的数据是: " << shared_data << std::endl;sleep(2); // 模拟写入操作pthread_rwlock_unlock(&rwlock); // 解锁}delete (int*)arg;return NULL;
}int main()
{srand(time(nullptr)^getpid());pthread_rwlock_init(&rwlock, NULL); // 初始化读写锁// 可以更高读写数量配比,观察现象const int reader_num = 2;const int writer_num = 2;const int total = reader_num + writer_num;pthread_t threads[total]; // 假设读者和写者数量相等// 创建读者线程for (int i = 0; i < reader_num; ++i){int *id = new int(i);pthread_create(&threads[i], NULL, Reader, id);}// 创建写者线程for (int i = reader_num; i < total; ++i){int *id = new int(i - reader_num);pthread_create(&threads[i], NULL, Writer, id);}// 等待所有线程完成for (int i = 0; i < total; ++i){pthread_join(threads[i], NULL);}pthread_rwlock_destroy(&rwlock); // 销毁读写锁return 0;
}
读者优先(Reader-Preference)
在这种策略中,系统会尽可能多地允许多个读者同时访问资源(比如共享文件或数 据),而不会优先考虑写者。这意味着当有读者正在读取时,新到达的读者会立即被 允许进入读取区,而写者则会被阻塞,直到所有读者都离开读取区。读者优先策略可 能会导致写者饥饿(即写者长时间无法获得写入权限),特别是当读者频繁到达时。
写者优先(Writer-Preference)
在这种策略中,系统会优先考虑写者。当写者请求写入权限时,系统会尽快地让写者进入写入区,即使此时有读者正在读取。这通常意味着一旦有写者到达,所有后续的 读者都会被阻塞,直到写者完成写入并离开写入区。写者优先策略可以减少写者等待 的时间,但可能会导致读者饥饿(即读者长时间无法获得读取权限),特别是当写者频繁到达时。
五、自旋锁
5.1、概述
自旋锁是一种多线程同步机制,用于保护共享资源免受并发访问的影响。在多个线程 尝试获取锁时,它们会持续自旋(即在一个循环中不断检查锁是否可用)而不是立即 进入休眠状态等待锁的释放。这种机制减少了线程切换的开销,适用于短时间内锁的 竞争情况。但是不合理的使用,可能会造成 CPU 的浪费。
5.2、原理
自旋锁通常使用一个共享的标志位(如一个布尔值)来表示锁的状态。当标志位为true 时,表示锁已被某个线程占用;当标志位为 false 时,表示锁可用。当一个线程尝 试获取自旋锁时,它会不断检查标志位:
- 如果标志位为 false,表示锁可用,线程将设置标志位为 true,表示自己占用了锁,并进入临界区。
- 如果标志位为 true(即锁已被其他线程占用),线程会在一个循环中不断自旋等待,直到锁被释放。
5.3、优点与缺点
优点:
- 低延迟:自旋锁适用于短时间内的锁竞争情况,因为它不会让线程进入休眠状 态,从而避免了线程切换的开销,提高了锁操作的效率。
- 减少系统调度开销:等待锁的线程不会被阻塞,不需要上下文切换,从而减少了系统调度的开销。
缺点:
- CPU 资源浪费:如果锁的持有时间较长,等待获取锁的线程会一直循环等待,导 致 CPU 资源的浪费。
- 可能引起活锁:当多个线程同时自旋等待同一个锁时,如果没有适当的退避策略,可能会导致所有线程都在不断检查锁状态而无法进入临界区,形成活锁。
5.4、使用场景
- 短暂等待的情况:适用于锁被占用时间很短的场景,如多线程对共享数据进行简单的读写操作。
- 多线程锁使用:通常用于系统底层,同步多个 CPU 对共享资源的访问。
5.5、纯软件自旋锁类似的原理实现
自旋锁的实现通常使用原子操作来保证操作的原子性,常用的软件实现方式是通过CAS(Compare-And-Swap)指令实现。以下是一个简单的自旋锁实现示例(伪代码):
C++
#include <stdio.h>
#include <stdatomic.h>
#include <pthread.h>
#include <unistd.h>
// 使用原子标志来模拟自旋锁
atomic_flag spinlock = ATOMIC_FLAG_INIT; // ATOMIC_FLAG_INIT 是 0
// 尝试获取锁
void spinlock_lock() {
while (atomic_flag_test_and_set(&spinlock)) {
// 如果锁被占用,则忙等待
}
}
// 释放锁
void spinlock_unlock() {
atomic_flag_clear(&spinlock);
}
C++
typedef _Atomic struct
{
#if __GCC_ATOMIC_TEST_AND_SET_TRUEVAL == 1
_Bool __val;
#else
unsigned char __val;
#endif
} atomic_flag;
功能描述:
atomic_flag_test_and_set 函数检查 atomic_flag 的当前状态。如果atomic_flag 之前没有被设置过(即其值为 false 或“未设置”状态),则函数会将其 设置为 true(或“设置”状态),并返回先前的值(在这种情况下为 false)。如果atomic_flag 之前已经被设置过(即其值为 true),则函数不会改变其状态,但会返回 true。
原子性:
这个操作是原子的,意味着在多线程环境中,它保证了对 atomic_flag 的读取和修改是不可分割的。当一个线程调用此函数时,其他线程无法看到这个操作的任何 中间状态,这确保了操作的线程安全性。
Linux 提供的自旋锁系统调用:
C++
#include <pthread.h>
int pthread_spin_lock(pthread_spinlock_t *lock);
int pthread_spin_trylock(pthread_spinlock_t *lock);
int pthread_spin_unlock(pthread_spinlock_t *lock);
int pthread_spin_init(pthread_spinlock_t *lock, int pshared);
int pthread_spin_destroy(pthread_spinlock_t *lock);
注意:
- 在使用自旋锁时,需要确保锁被释放的时间尽可能短,以避免 CPU 资源的浪费。
- 在多 CPU 环境下,自旋锁可能不如其他锁机制高效,因为它可能导致线程在不同的 CPU 上自旋等待。
结论:
自旋锁是一种适用于短时间内锁竞争情况的同步机制,它通过减少线程切换的开销来提高锁操作的效率。然而,它也存在 CPU 资源浪费和可能引起活锁等缺点。在使用自旋锁时,需要根据具体的应用场景进行选择,并确保锁被释放的时间尽可能短。
示例代码:
// 操作共享变量会有问题的售票系统代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>int ticket = 1000;
pthread_spinlock_t lock;void *route(void *arg)
{char *id = (char *)arg;while (1){pthread_spin_lock(&lock);if (ticket > 0){usleep(1000);printf("%s sells ticket:%d\n", id, ticket);ticket--;pthread_spin_unlock(&lock);}else{pthread_spin_unlock(&lock);break;}}return nullptr;
}int main(void)
{pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE);pthread_t t1, t2, t3, t4;pthread_create(&t1, NULL, route, (void *)"thread 1");pthread_create(&t2, NULL, route, (void *)"thread 2");pthread_create(&t3, NULL, route, (void *)"thread 3");pthread_create(&t4, NULL, route, (void *)"thread 4");pthread_join(t1, NULL);pthread_join(t2, NULL);pthread_join(t3, NULL);pthread_join(t4, NULL);pthread_spin_destroy(&lock);return 0;
}