1、程序简介
该程序是基于OpenHarmony的C++公共基础类库的读写锁:rwlock。
本案例主要完成如下工作:
本案例已基于凌蒙派-RK3568开发板验证过,如有需要相关代码,请参考:
https://gitee.com/Lockzhiner-Electronics/lockzhiner-rk3568-openharmony/tree/master/samples/a25_utils_rwlock
2、基础知识
C公共基础类库为标准系统提供了一些常用的C开发工具类,包括:
- 文件、路径、字符串相关操作的能力增强接口
- 读写锁、信号量、定时器、线程增强及线程池等接口
- 安全数据容器、数据序列化等接口
- 各子系统的错误码相关定义
2.1、添加C++公共基础类库依赖
修改需调用模块的BUILD.gn,在external_deps或deps中添加如下:
一般而言,我们只需要填写"c_utils:utils"即可。
2.2、rwlock头文件
C++公共基础类库的rwlock头文件在://commonlibrary/c_utils/base/include/rwlock.h
可在源代码中添加如下:
OpenHarmony读写锁分为如下三大类:
(1)RWLock类
(2)UniqueWriteGuard类
(3)UniqueReadGuard类
2.3、OHOS::Utils::RWLock接口说明
2.3.1、RWLock
构造函数。
参数说明:
参数名称 |
类型 |
参数说明 |
writeFirst |
bool |
是否优先写模式 |
2.3.2、~RWLock
析构函数。
2.3.3、LockRead
获取读锁。
2.3.4、UnLockRead
释放读锁。
2.3.5、LockWrite
获取写锁。
2.3.6、UnLockWrite
获取写锁。
2.4、OHOS::Utils::UniqueWriteGuard接口说明
2.4.1、UniqueWriteGuard
构造函数。
参数说明:
参数名称 |
类型 |
参数说明 |
rwLockable |
RWLockable |
template <typename RWLockable> |
2.4.2、~UniqueWriteGuard
析构函数。
2.5、OHOS::Utils::UniqueReadGuard接口说明
2.5.1、UniqueReadGuard
构造函数。
参数说明:
参数名称 |
类型 |
参数说明 |
rwLockable |
RWLockable |
template <typename RWLockable> |
2.5.2、~UniqueReadGuard
析构函数。
3、程序解析
3.1、创建编译引导
在上一级目录BUILD.gn文件添加一行编译引导语句。
"a25_utils_rwlock:utils_rwlock",
该行语句表示引入 参与编译。
3.2、创建编译项目
创建a25_utils_rwlock目录,并添加如下文件:
3.3、创建BUILD.gn
编辑BUILD.gn文件。
注意:
(1)BUILD.gn中所有的TAB键必须转化为空格,否则会报错。如果自己不知道如何规范化,可以:
3.4、创建源代码
3.4.1、创建读写锁和公共资源变量
3.4.2、创建线程池并设置
3.4.3、启动3个读线程和3个写线程
调用AddTask()添加子线程。
3.4.4、编写读线程
读线程完成如下步骤:
- 调用LockRead()获取读锁;
- 如果获取到读锁,打印公共资源变量,并睡眠1秒;
- 调用UnLockRead()释放读锁;
- 睡眠1秒;
- 上述操作循环5次;
3.4.5、编写写线程
写线程完成如下步骤:
- 调用LockWrite()获取写锁;
- 如果获取到读锁,公共资源变量累加1,并睡眠1秒;
- 调用UnLockWrite()释放写锁;
- 睡眠1秒;
- 上述操作循环5次;
4、编译步骤
进入OpenHarmony编译环境,运行命令:
5、运行结果
# utils_rwlock
2017-8-5 20:7:8, thread_read_0: start
2017-8-5 20:7:8, thread_read_0: LockRead wait...
2017-8-5 20:7:8, thread_read_0: LockRead success and read count = 0 and sleep 1 sec
2017-8-5 20:7:8, thread_read_1: start
2017-8-5 20:7:8, thread_read_1: LockRead wait...
2017-8-5 20:7:8, thread_read_1: LockRead success and read count = 0 and sleep 1 sec
2017-8-5 20:7:8, thread_read_2: start
2017-8-5 20:7:8, thread_read_2: LockRead wait...
2017-8-5 20:7:8, thread_read_2: LockRead success and read count = 0 and sleep 1 sec
2017-8-5 20:7:8, thread_write_0: start
2017-8-5 20:7:8, thread_write_0: LockWrite wait...
2017-8-5 20:7:8, thread_write_1: start
2017-8-5 20:7:8, thread_write_1: LockWrite wait...
2017-8-5 20:7:9, thread_read_0: UnLockRead
2017-8-5 20:7:9, thread_read_1: UnLockRead
2017-8-5 20:7:9, thread_read_2: UnLockRead
2017-8-5 20:7:9, thread_write_1: LockWrite success and write count++ and sleep 1 sec
2017-8-5 20:7:10, thread_read_0: LockRead wait...
2017-8-5 20:7:10, thread_write_1: UnLockWrite
2017-8-5 20:7:10, thread_write_0: LockWrite success and write count++ and sleep 1 sec
2017-8-5 20:7:10, thread_read_1: LockRead wait...
2017-8-5 20:7:10, thread_read_2: LockRead wait...
2017-8-5 20:7:112017-8-5 20:7:11, thread_write_0, thread_read_0: LockRead success and read count = : UnLockWrite
2 and sleep 1 sec
2017-8-5 20:7:11, thread_read_1: LockRead success and read count = 2 and sleep 1 sec
2017-8-5 20:7:11, thread_write_1: LockWrite wait...
2017-8-5 20:7:12, thread_write_0: LockWrite wait...
2017-8-5 20:7:12, thread_read_1: UnLockRead
2017-8-5 20:7:12, thread_read_0: UnLockRead
2017-8-5 20:7:12, thread_write_0: LockWrite success and write count++ and sleep 1 sec
2017-8-5 20:7:13, thread_read_1: LockRead wait...
2017-8-5 20:7:13, thread_read_0: LockRead wait...
2017-8-5 20:7:13, thread_write_0: UnLockWrite
2017-8-5 20:7:13, thread_write_1: LockWrite success and write count++ and sleep 1 sec
2017-8-5 20:7:14, thread_write_0: LockWrite wait...
2017-8-5 20:7:14, thread_write_1: UnLockWrite
2017-8-5 20:7:14, thread_write_0: LockWrite success and write count++ and sleep 1 sec
2017-8-5 20:7:15, thread_read_0: LockRead success and read count = 2017-8-5 20:7:15, thread_write_0: UnLockWrite
2017-8-5 20:7:15, thread_write_1: LockWrite wait...
2017-8-5 20:7:15, thread_read_1: LockRead success and read count = 5 and sleep 1 sec
5 and sleep 1 sec
2017-8-5 20:7:16, thread_write_0: LockWrite wait...
2017-8-5 20:7:16, thread_read_1: UnLockRead
2017-8-5 20:7:16, thread_read_0: UnLockRead
2017-8-5 20:7:16, thread_write_1: LockWrite success and write count++ and sleep 1 sec
2017-8-5 20:7:17, thread_read_1: LockRead wait...
2017-8-5 20:7:17, thread_read_0: LockRead wait...
2017-8-5 20:7:17, thread_write_1: UnLockWrite
2017-8-5 20:7:17, thread_write_0: LockWrite success and write count++ and sleep 1 sec
2017-8-5 20:7:18, thread_write_1: LockWrite wait...
2017-8-5 20:7:18, thread_write_1: LockWrite success and write count++ and sleep 1 sec2017-8-5 20:7:18,
thread_write_0: UnLockWrite
2017-8-5 20:7:192017-8-5 20:7:19, thread_write_0, thread_write_1: UnLockWrite: LockWrite wait...2017-8-5 20:7:19,
thread_read_1: LockRead success and read count = 8 and sleep 1 sec
2017-8-5 20:7:19, thread_read_0: LockRead success and read count = 8 and sleep 1 sec
2017-8-5 20:7:19, thread_read_2: LockRead success and read count = 8 and sleep 1 sec
2017-8-5 20:7:20, thread_write_1: LockWrite wait...
2017-8-5 20:7:20, thread_read_1: UnLockRead
2017-8-5 20:7:20, thread_read_0: UnLockRead
2017-8-5 20:7:20, thread_read_2: UnLockRead
2017-8-5 20:7:20, thread_write_1: LockWrite success and write count++ and sleep 1 sec
2017-8-5 20:7:21, thread_read_1: LockRead wait...
2017-8-5 20:7:21, thread_read_0: LockRead wait...
2017-8-5 20:7:21, thread_read_2: LockRead wait...
2017-8-5 20:7:21, thread_write_1: UnLockWrite
2017-8-5 20:7:21, thread_write_0: LockWrite success and write count++ and sleep 1 sec
2017-8-5 20:7:22, thread_write_0: UnLockWrite
2017-8-5 20:7:22, thread_read_0: LockRead success and read count = 10 and sleep 1 sec
2017-8-5 20:7:22, thread_read_1: LockRead success and read count = 10 and sleep 1 sec
2017-8-5 20:7:22, thread_read_2: LockRead success and read count = 10 and sleep 1 sec
2017-8-5 20:7:23, thread_read_0: UnLockRead
2017-8-5 20:7:23, thread_read_1: UnLockRead
2017-8-5 20:7:23, thread_read_2: UnLockRead
2017-8-5 20:7:24, thread_read_2: LockRead wait...
2017-8-5 20:7:24, thread_read_2: LockRead success and read count = 10 and sleep 1 sec
2017-8-5 20:7:25, thread_read_2: UnLockRead
2017-8-5 20:7:26, thread_read_2: LockRead wait...
2017-8-5 20:7:26, thread_read_2: LockRead success and read count = 10 and sleep 1 sec
2017-8-5 20:7:27, thread_read_2: UnLockRead
#
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.