该程序是基于OpenHarmony的C++公共基础类库的读写锁:rwlock。
本案例主要完成如下工作:
本案例已基于凌蒙派-RK3568开发板验证过,如有需要相关代码,请参考:
C++公共基础类库为标准系统提供了一些常用的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"即可。
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
构造函数。
RWLock() : RWLock(true);
RWLock(bool writeFirst);
参数说明:
参数名称 | 类型 | 参数说明 |
---|---|---|
writeFirst | bool | 是否优先写模式 |
析构函数。
~RWLock();
获取读锁。
void LockRead();
释放读锁。
void UnLockRead();
获取写锁。
void LockWrite();
获取写锁。
void UnLockWrite();
构造函数。
UniqueWriteGuard(RWLockable &rwLockable)
参数说明:
参数名称 | 类型 | 参数说明 |
---|---|---|
rwLockable | RWLockable | template |
析构函数。
~UniqueWriteGuard();
构造函数。
UniqueReadGuard(RWLockable &rwLockable)
参数说明:
参数名称 | 类型 | 参数说明 |
---|---|---|
rwLockable | RWLockable | template |
析构函数。
~UniqueReadGuard();
在上一级目录BUILD.gn文件添加一行编译引导语句。
import("//build/ohos.gni")
group("samples") {
deps = [
"a25_utils_rwlock:utils_rwlock", # 添加该行
]
}
"a25_utils_rwlock:utils_rwlock",
该行语句表示引入 参与编译。
创建a25_utils_rwlock目录,并添加如下文件:
a25_utils_rwlock
├── utils_rwlock_sample.cpp # .cpp源代码
├── BUILD.gn # 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
static OHOS::Utils::RWLock m_rwlock(true);
static int m_count = 0;
int main(int argc, char **argv)
{
OHOS::ThreadPool threads("name_semaphore_threads");
int threads_start = 6;
......
threads.SetMaxTaskNum(128);
threads.Start(threads_start);
......
}
调用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);
}
读线程完成如下步骤:
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);
}
}
写线程完成如下步骤:
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);
}
}
进入OpenHarmony编译环境,运行命令:
hb build -f
# 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
#