基于OpenHarmony标准系统的C++公共基础类库案例:rwlock 原创
1、程序简介
该程序是基于OpenHarmony的C++公共基础类库的读写锁:rwlock。
本案例主要完成如下工作:
-
创建3个读线程,每个读线程循环5次,每次循环获取读锁,将公共资源变量打印,睡眠1秒,然后释放读锁,最后再睡眠1秒。
-
创建3个写线程,每个写线程循环5次,每次循环获取写锁,将公共资源变量打印,睡眠1秒,然后释放读锁,最后再睡眠1秒。
本案例已基于凌蒙派-RK3568开发板验证过,如有需要相关代码,请参考:
2、基础知识
C++公共基础类库为标准系统提供了一些常用的C++开发工具类,包括:
- 文件、路径、字符串相关操作的能力增强接口
- 读写锁、信号量、定时器、线程增强及线程池等接口
- 安全数据容器、数据序列化等接口
- 各子系统的错误码相关定义
2.1、添加C++公共基础类库依赖
修改需调用模块的BUILD.gn,在external_deps或deps中添加如下:
ohos_shared_library("xxxxx") {
...
external_deps = [
...
# 动态库依赖(可选)
"c_utils:utils",
# 静态库依赖(可选)
"c_utils:utilsbase",
# Rust动态库依赖(可选)
"c_utils:utils_rust",
]
...
}
一般而言,我们只需要填写"c_utils:utils"即可。
2.2、rwlock头文件
C++公共基础类库的rwlock头文件在://commonlibrary/c_utils/base/include/rwlock.h
可在源代码中添加如下:
#include <rwlock.h>
OpenHarmony读写锁分为如下三大类:
(1)RWLock类
OHOS::RWLock
(2)UniqueWriteGuard类
OHOS::UniqueWriteGuard
(3)UniqueReadGuard类
OHOS::UniqueReadGuard
2.3、OHOS::Utils::RWLock接口说明
2.3.1、RWLock
构造函数。
RWLock() : RWLock(true);
RWLock(bool writeFirst);
参数说明:
参数名称 | 类型 | 参数说明 |
---|---|---|
writeFirst | bool | 是否优先写模式 |
2.3.2、~RWLock
析构函数。
~RWLock();
2.3.3、LockRead
获取读锁。
void LockRead();
2.3.4、UnLockRead
释放读锁。
void UnLockRead();
2.3.5、LockWrite
获取写锁。
void LockWrite();
2.3.6、UnLockWrite
获取写锁。
void UnLockWrite();
2.4、OHOS::Utils::UniqueWriteGuard接口说明
2.4.1、UniqueWriteGuard
构造函数。
UniqueWriteGuard(RWLockable &rwLockable)
参数说明:
参数名称 | 类型 | 参数说明 |
---|---|---|
rwLockable | RWLockable | template <typename RWLockable> |
2.4.2、~UniqueWriteGuard
析构函数。
~UniqueWriteGuard();
2.5、OHOS::Utils::UniqueReadGuard接口说明
2.5.1、UniqueReadGuard
构造函数。
UniqueReadGuard(RWLockable &rwLockable)
参数说明:
参数名称 | 类型 | 参数说明 |
---|---|---|
rwLockable | RWLockable | template <typename RWLockable> |
2.5.2、~UniqueReadGuard
析构函数。
~UniqueReadGuard();
3、程序解析
3.1、创建编译引导
在上一级目录BUILD.gn文件添加一行编译引导语句。
import("//build/ohos.gni")
group("samples") {
deps = [
"a25_utils_rwlock:utils_rwlock", # 添加该行
]
}
"a25_utils_rwlock:utils_rwlock",
该行语句表示引入 参与编译。
3.2、创建编译项目
创建a25_utils_rwlock目录,并添加如下文件:
a25_utils_rwlock
├── utils_rwlock_sample.cpp # .cpp源代码
├── BUILD.gn # GN文件
3.3、创建BUILD.gn
编辑BUILD.gn文件。
import("//build/ohos.gni")
ohos_executable("utils_rwlock") {
sources = [ "utils_rwlock_sample.cpp" ]
include_dirs = [
"//commonlibrary/c_utils/base/include",
"//commonlibrary/c_utils/base:utils",
"//third_party/googletest:gtest_main",
"//third_party/googletest/googletest/include"
]
external_deps = [
"c_utils:utils"
]
part_name = "product_rk3568"
install_enable = true
}
注意:
(1)BUILD.gn中所有的TAB键必须转化为空格,否则会报错。如果自己不知道如何规范化,可以:
# 安装gn工具
sudo apt-get install ninja-build
sudo apt install generate-ninja
# 规范化BUILD.gn
gn format BUILD.gn
3.4、创建源代码
3.4.1、创建读写锁和公共资源变量
static OHOS::Utils::RWLock m_rwlock(true);
static int m_count = 0;
3.4.2、创建线程池并设置
int main(int argc, char **argv)
{
OHOS::ThreadPool threads("name_semaphore_threads");
int threads_start = 6;
......
threads.SetMaxTaskNum(128);
threads.Start(threads_start);
......
}
3.4.3、启动3个读线程和3个写线程
调用AddTask()添加子线程。
// 启动读线程
for (int i = 0; i < 3; i++) {
str_name = "thread_read_" + to_string(i);
auto task = std::bind(funcRead, str_name);
threads.AddTask(task);
}
// 启动写线程
for (int i = 0; i < 3; i++) {
str_name = "thread_write_" + to_string(i);
auto task = std::bind(funcWrite, str_name);
threads.AddTask(task);
}
3.4.4、编写读线程
读线程完成如下步骤:
- 调用LockRead()获取读锁;
- 如果获取到读锁,打印公共资源变量,并睡眠1秒;
- 调用UnLockRead()释放读锁;
- 睡眠1秒;
- 上述操作循环5次;
static void funcRead(const string& name)
{
cout << get_curtime() << ", " << name << ": start\n";
for (int i = 0; i < 5; i++) {
cout << get_curtime() << ", " << name << ": LockRead wait..." << endl;
m_rwlock.LockRead();
cout << get_curtime() << ", " << name << ": LockRead success and read count = " << m_count << " and sleep 1 sec" << endl;
sleep(1);
m_rwlock.UnLockRead();
cout << get_curtime() << ", " << name << ": UnLockRead" << endl;
sleep(1);
}
}
3.4.5、编写写线程
写线程完成如下步骤:
- 调用LockWrite()获取写锁;
- 如果获取到读锁,公共资源变量累加1,并睡眠1秒;
- 调用UnLockWrite()释放写锁;
- 睡眠1秒;
- 上述操作循环5次;
static void funcWrite(const string& name)
{
cout << get_curtime() << ", " << name << ": start\n";
for (int i = 0; i < 5; i++) {
cout << get_curtime() << ", " << name << ": LockWrite wait..." << endl;
m_rwlock.LockWrite();
cout << get_curtime() << ", " << name << ": LockWrite success and write count++ and sleep 1 sec" << endl;
m_count++;
sleep(1);
m_rwlock.UnLockWrite();
cout << get_curtime() << ", " << name << ": UnLockWrite" << endl;
sleep(1);
}
}
4、编译步骤
进入OpenHarmony编译环境,运行命令:
hb build -f
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
#