点击↑深色口袋物联,选择关注公众号,获取更多内容,不迷路
在glibc2.35中,pthread线程提供了5种同步机制,分别为条件变量、互斥锁、读写锁、自旋锁和屏障,本篇主要回顾读写锁
在glibc2.35中,读写锁的定义在sysdeps/nptl/bits/pthreadtypes.h中pthread_rwlock_t,与互斥锁相似,
typedef union{ struct __pthread_rwlock_arch_t __data; char __size[__SIZEOF_PTHREAD_RWLOCK_T]; long int __align;} pthread_rwlock_t;其中__data是真实的数据载体,包含了操作系统所需要的全部底层信息,__size,固定大小的字符数组,保证新旧版本的二进制兼容,以及一个长整型变量,用于控制内存对齐
读写锁的核心作用:区分「读操作」和「写操作」,实现「多读共享、单写排他」的访问控制 ——
简单说:读写锁是「互斥锁的升级版」,解决了互斥锁「无论读写都串行」的性能问题,在「多读少写」场景下,并发性能比互斥锁提升数倍甚至数十倍。
互斥锁的核心问题是**「一刀切」**:无论线程是读还是写,都只能串行访问,而实际开发中,80% 的场景是「读多写少」(如配置读取、数据查询、日志查看),互斥锁会浪费大量并发性能:
读写锁的核心价值是**「多读并行」**,所有场景都围绕「多读少写」展开,覆盖 Linux 应用层开发的高频优化场景,在匹配场景下,是性能最优的同步方案,按使用频率排序如下:
读写锁的接口全部在 <pthread.h> 头文件中,编译时必须加 -lpthread 链接线程库,所有接口返回值:成功返回 0,失败返回错误码(非 errno,需手动处理)。
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);rwlock:待初始化的读写锁地址;attr:读写锁属性(如优先级、共享范围),传NULL使用默认属性即可;pthread_rwlock_destroy销毁!int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);// 读模式超时加锁int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abstime);// 写模式超时加锁int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abstime);int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);二者是「互补关系」,按需选择即可,核心差异如下:
✅ 核心结论:多读少写用读写锁,其他场景用互斥锁。
✅ 核心结论:只读用读锁,写操作用写锁,禁止混用。
自旋锁是「忙等锁」,和读写锁的核心差异在「阻塞方式」:
✅ 选型原则:锁持有时间长 + 多读少写→读写锁;锁持有时间极短→自旋锁。
条件变量是「同步机制」,读写锁是「互斥机制」,核心差异如下:
✅ 核心结论:保护共享资源用读写锁 / 互斥锁,控制执行顺序用条件变量。
代码才是硬道理。下面三个例子,涵盖了基础使用、写者饥饿演示和非阻塞应用,直接拷贝到你的 ARM Linux 开发板上就能跑(记得编译加 -lpthread)。
最典型的用法。多个线程读取配置,一个线程更新配置。可以调整延时,模块执行时长
#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <unistd.h>// 模拟配置表char shared_config[1024] = "Initial Config: v1.0";pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;// 读者线程:频繁读取配置void* reader_thread(void *arg) { int id = *(int*)arg; while (1) { pthread_rwlock_rdlock(&rwlock); // 加读锁 printf("[读者 %d] 读取配置: %s\n", id, shared_config); pthread_rwlock_unlock(&rwlock); usleep(100000); // 100ms 模拟处理耗时 } return NULL;}// 写者线程:偶尔更新配置void* writer_thread(void *arg) { int ver = 1; while (1) { sleep(2); // 每2秒更新一次 pthread_rwlock_wrlock(&rwlock); // 加写锁(独占) sprintf(shared_config, "Updated Config: v1.%d", ver++); printf("[写者] 配置已更新!\n"); pthread_rwlock_unlock(&rwlock); } return NULL;}int main() { pthread_t readers[3], writer; int ids[3] = {1, 2, 3}; // 启动3个读者 for(int i=0; i<3; i++) { pthread_create(&readers[i], NULL, reader_thread, &ids[i]); } // 启动1个写者 pthread_create(&writer, NULL, writer_thread, NULL); // 运行5秒 sleep(5); // 实际工程中这里应该设置退出标志并 join,这里简化处理直接退出 printf("主程序退出\n"); return 0;}运行结果如下,5s内写了2次(每2秒执行1次)
[读者 1] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 1] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[读者 1] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 1] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[读者 1] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 1] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[读者 1] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 1] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[读者 1] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 1] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 1] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[读者 1] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[读者 1] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[读者 1] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[读者 1] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[读者 1] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 1] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 1] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 1] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[读者 1] 读取配置: Initial Config: v1.0[读者 3] 读取配置: Initial Config: v1.0[读者 2] 读取配置: Initial Config: v1.0[写者] 配置已更新![读者 3] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 3] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 3] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 3] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 3] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 3] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 3] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 3] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 3] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 3] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 3] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 3] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 3] 读取配置: Updated Config: v1.1[读者 3] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 3] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 3] 读取配置: Updated Config: v1.1[读者 3] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 3] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 3] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 1] 读取配置: Updated Config: v1.1[读者 2] 读取配置: Updated Config: v1.1[读者 3] 读取配置: Updated Config: v1.1[写者] 配置已更新![读者 2] 读取配置: Updated Config: v1.2[读者 3] 读取配置: Updated Config: v1.2[读者 1] 读取配置: Updated Config: v1.2[读者 1] 读取配置: Updated Config: v1.2[读者 3] 读取配置: Updated Config: v1.2[读者 2] 读取配置: Updated Config: v1.2[读者 1] 读取配置: Updated Config: v1.2[读者 2] 读取配置: Updated Config: v1.2[读者 3] 读取配置: Updated Config: v1.2[读者 1] 读取配置: Updated Config: v1.2[读者 3] 读取配置: Updated Config: v1.2[读者 2] 读取配置: Updated Config: v1.2[读者 3] 读取配置: Updated Config: v1.2[读者 1] 读取配置: Updated Config: v1.2[读者 2] 读取配置: Updated Config: v1.2[读者 3] 读取配置: Updated Config: v1.2[读者 1] 读取配置: Updated Config: v1.2[读者 2] 读取配置: Updated Config: v1.2[读者 1] 读取配置: Updated Config: v1.2[读者 3] 读取配置: Updated Config: v1.2[读者 2] 读取配置: Updated Config: v1.2[读者 1] 读取配置: Updated Config: v1.2[读者 2] 读取配置: Updated Config: v1.2[读者 3] 读取配置: Updated Config: v1.2[读者 2] 读取配置: Updated Config: v1.2[读者 1] 读取配置: Updated Config: v1.2[读者 3] 读取配置: Updated Config: v1.2[读者 2] 读取配置: Updated Config: v1.2[读者 1] 读取配置: Updated Config: v1.2[读者 3] 读取配置: Updated Config: v1.2主程序退出这个例子演示读写锁的缺点:如果读者源源不断,写者可能会拿不到锁。
#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <unistd.h>int data = 0;pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;// 大量的“快餐”读者void* reader_thread(void *arg) { while (1) { pthread_rwlock_rdlock(&rwlock); // 模拟很快读一下 // int val = data; usleep(10000); // 10ms pthread_rwlock_unlock(&rwlock); } return NULL;}// 苦逼的写者void* writer_thread(void *arg) { int count = 0; while (1) { printf("[写者] 等待写锁...\n"); pthread_rwlock_wrlock(&rwlock); printf("[写者] 拿到锁了!更新数据 count=%d\n", ++count); data = count; sleep(1); // 模拟写操作耗时 pthread_rwlock_unlock(&rwlock); } return NULL;}int main() { pthread_t r1, r2, r3, w; // 启动3个疯狂的读者 pthread_create(&r1, NULL, reader_thread, NULL); pthread_create(&r2, NULL, reader_thread, NULL); pthread_create(&r3, NULL, reader_thread, NULL); // 启动1个苦逼写者 pthread_create(&w, NULL, writer_thread, NULL); sleep(10); printf("主程序退出\n"); return 0;}运行这个你会发现,写者的日志“拿到锁了”出现得非常少,大部分时间都在排队,这就是写者饥饿。

在嵌入式 BSP 开发中,我们经常不能死等锁。比如看门狗线程要检查状态,如果锁住了我就先跳过,别把狗饿死了。
#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <unistd.h>int sensor_map[10];pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;// 检查线程(非阻塞)void* watchdog_thread(void *arg) { while (1) { int ret = pthread_rwlock_tryrdlock(&rwlock); if (ret == 0) { // 成功拿到锁,检查数据 printf("[看门狗] 检查状态: map[0]=%d\n", sensor_map[0]); pthread_rwlock_unlock(&rwlock); } else { // 锁被占用(可能在写),不等待,直接跳过本次检查 printf("[看门狗] 锁忙,跳过本次检查\n"); } sleep(1); } return NULL;}// 更新线程void* writer_thread(void *arg) { int val = 0; while (1) { pthread_rwlock_wrlock(&rwlock); for(int i=0; i<10; i++) sensor_map[i] = ++val; printf("[写者] 地图更新完成\n"); sleep(3); // 模拟长写操作,故意占锁久一点 pthread_rwlock_unlock(&rwlock); } return NULL;}int main() { pthread_t w, dog; pthread_create(&w, NULL, writer_thread, NULL); pthread_create(&dog, NULL, watchdog_thread, NULL); sleep(10); return 0;}运行结果

✔️ 读写锁的优点是压倒性的:在多读少写场景下,是性能最优的同步方案,没有任何技术能替代;
✔️ 读写锁的缺点都是可规避的:通过设置写优先、减小锁粒度、规范加解锁逻辑,能轻松解决所有局限性;
✔️ 结论:读写锁是 Linux 高并发读场景「必学、必会、必用」的优化技术,核心是「场景匹配」。