读写锁(rwlock)与互斥锁(Mutex Lock)相比,有如下特点:
- 更加细致的区分了读和写,给共享资源分别上了“读”锁和“写”锁。
- “写”独占,“读”共享,“写”锁优先级更高
- 如果共享资源在绝大多数情况下是“读”操作,可以提高步伐的并发性能。
常用的函数原型总结如下:- pthread_rwlock_t rwlock (= PTHREAD_RWLOCK_INITIALIZER); // 定义读写锁的变量并初始化,一定要全局变量,如果使用了下述初始化函数,括号内静态初始化就可以取消
- pthread_rwlock_init(&rwlock, NULL); // const pthread_rwlockattr_t *restrict attr,第二个参数默认为NULL
- pthread_rwlock_rdlock(&rwlock); // 上读锁,并进行读操作
- pthread_rwlock_wrlock(&rwlock); // 上写锁,并进行写操作,同一时间只有一把写锁,并与其他锁互斥
- pthread_rwlock_unlock(&rwlock); // 解锁,读和写的解锁函数都相同
- pthread_rwlock_destroy(&rwlock); // 不用了就销毁读写锁
复制代码 设计一个步伐,步伐中有3个线程,主线程A创建一个文本,每隔5s获取一次系统时间并写入到该文本中,另外两个线程B和C分别从文本中读取当前的时间和日期,子线程B输出系统时间"hh:mm:ss",子线程c输出系统日期"2024年05月31日”,要求使用读写锁实现互斥。 提示:主线程A获取写操作的锁,另外的线程分别获取读操作的锁,步伐如下:- #include <stdio.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <time.h>
- FILE *timefp; // 以文件句柄为共享区
- pthread_rwlock_t rwlock; // 定义读写锁的变量,一定要全局变量
- // 需要用到的时间结构体,提前列出
- // struct tm {
- // int tm_sec; /* Seconds (0-60) */
- // int tm_min; /* Minutes (0-59) */
- // int tm_hour; /* Hours (0-23) */
- // int tm_mday; /* Day of the month (1-31) */
- // int tm_mon; /* Month (0-11) */
- // int tm_year; /* Year - 1900 */
- // int tm_wday; /* Day of the week (0-6, Sunday = 0) */
- // int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */
- // int tm_isdst; /* Daylight saving time */
- // }
- void *funcB(void *arg) // B线程
- {
- time_t tlocB = 0;
- while (1)
- {
- pthread_rwlock_rdlock(&rwlock); // 上锁,进行读操作
- timefp = fopen("/home/rice/0531/time.txt", "r+");
- fread(&tlocB, sizeof(time_t), 1, timefp);
- struct tm *tt = localtime(&tlocB);
- printf("I am thread B,the time is ===== %02d:%02d:%02d\n", tt->tm_hour, tt->tm_min, tt->tm_sec);
- pthread_rwlock_unlock(&rwlock); // 解锁,释放权限
- fclose(timefp);
- sleep(2); // 每隔两秒进行一次输出
- }
- }
- void *funcC(void *arg)
- {
- time_t tlocC = 0;
- while (1)
- {
- pthread_rwlock_rdlock(&rwlock); // 上锁,进行读操作
- timefp = fopen("/home/rice/0531/time.txt", "rb+");
- fread(&tlocC, sizeof(time_t), 1, timefp);
- struct tm *tt = localtime(&tlocC);
- printf("I am thread C,the date is ===== %d年:%02d月:%02d日\n", tt->tm_year + 1900, tt->tm_mon + 1, tt->tm_mday);
- pthread_rwlock_unlock(&rwlock); // 解锁,释放权限
- fclose(timefp);
- sleep(2); // 每隔两秒进行一次输出
- }
- }
- int main()
- {
- pthread_rwlock_init(&rwlock, NULL); // 初始化读写锁
- pthread_t tidB, tidC;
- pthread_create(&tidB, NULL, funcB, NULL); // 创建线程
- pthread_create(&tidC, NULL, funcC, NULL);
- while (1) // 主线程每隔5秒获取一次系统时间
- {
- pthread_rwlock_wrlock(&rwlock); // 上锁,进行写操作
- timefp = fopen("/home/rice/0531/time.txt", "wb+"); // 打开文件,不存在就创建
- time_t tloc = time(NULL);
- fwrite(&tloc, sizeof(time_t), 1, timefp); // 将time原始数据存入文件
- fclose(timefp);
- pthread_rwlock_unlock(&rwlock); // 解锁,释放文件权限
- sleep(5); // 每隔5秒进行一次写入操作
- }
- // 等待B和C线程结束
- pthread_join(tidB, NULL);
- pthread_join(tidC, NULL);
- // 销毁读写锁
- pthread_rwlock_destroy(&rwlock);
- return 0;
- }
复制代码 输出结果如下:- I am thread B,the time is ===== 20:10:57
- I am thread C,the date is ===== 2024年:06月:01日
- I am thread B,the time is ===== 20:10:57
- I am thread C,the date is ===== 2024年:06月:01日
- I am thread C,the date is ===== 2024年:06月:01日
- I am thread B,the time is ===== 20:10:57
- I am thread B,the time is ===== 20:11:02
- I am thread C,the date is ===== 2024年:06月:01日
- I am thread B,the time is ===== 20:11:02
- I am thread C,the date is ===== 2024年:06月:01日
- ...
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |