0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

基于OpenHarmony标准系统的C++公共基础类库案例:rwlock

福州市凌睿智捷电子有限公司 2024-08-30 12:42 次阅读

1、程序简介

该程序是基于OpenHarmonyC++公共基础类库的读写锁:rwlock。

本案例主要完成如下工作:

创建3个读线程,每个读线程循环5次,每次循环获取读锁,将公共资源变量打印,睡眠1秒,然后释放读锁,最后再睡眠1秒。

创建3个写线程,每个写线程循环5次,每次循环获取写锁,将公共资源变量打印,睡眠1秒,然后释放读锁,最后再睡眠1秒。

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

OpenHarmony读写锁分为如下三大类:

(1)RWLock类

OHOS::RWLock

(2)UniqueWriteGuard类

OHOS::UniqueWriteGuard

(3)UniqueReadGuard类

OHOS::UniqueReadGuard

2.3、OHOS::RWLock接口说明

2.3.1、RWLock

构造函数。

RWLock() : RWLock(true);RWLock(bool writeFirst);

参数说明:

参数名称类型参数说明
writeFirstbool是否优先写模式

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::UniqueWriteGuard接口说明

2.4.1、UniqueWriteGuard

构造函数。

UniqueWriteGuard(RWLockable &rwLockable)

参数说明:

参数名称类型参数说明
rwLockableRWLockabletemplate

2.4.2、~UniqueWriteGuard

析构函数。

~UniqueWriteGuard();

2.5、OHOS::UniqueReadGuard接口说明

2.5.1、UniqueReadGuard

构造函数。

UniqueReadGuard(RWLockable &rwLockable)

参数说明:

参数名称类型参数说明
rwLockableRWLockabletemplate

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-buildsudo apt install generate-ninja# 规范化BUILD.gngn format BUILD.gn

3.4、创建源代码

3.4.1、创建读写锁和公共资源变量

static OHOS::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_rwlock2017-8-5 208, thread_read_0: start2017-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 sec2017-8-5 20:7:8, thread_read_1: start2017-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 sec2017-8-5 20:7:8, thread_read_2: start2017-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 sec2017-8-5 20:7:8, thread_write_0: start2017-8-5 20:7:8, thread_write_0: LockWrite wait...2017-8-5 20:7:8, thread_write_1: start2017-8-5 20:7:8, thread_write_1: LockWrite wait...2017-8-5 20:7:9, thread_read_0: UnLockRead2017-8-5 20:7:9, thread_read_1: UnLockRead2017-8-5 20:7:9, thread_read_2: UnLockRead2017-8-5 20:7:9, thread_write_1: LockWrite success and write count++ and sleep 1 sec2017-8-5 20:7:10, thread_read_0: LockRead wait...2017-8-5 20:7:10, thread_write_1: UnLockWrite2017-8-5 20:7:10, thread_write_0: LockWrite success and write count++ and sleep 1 sec2017-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 = : UnLockWrite2 and sleep 1 sec2017-8-5 20:7:11, thread_read_1: LockRead success and read count = 2 and sleep 1 sec2017-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: UnLockRead2017-8-5 20:7:12, thread_read_0: UnLockRead2017-8-5 20:7:12, thread_write_0: LockWrite success and write count++ and sleep 1 sec2017-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: UnLockWrite2017-8-5 20:7:13, thread_write_1: LockWrite success and write count++ and sleep 1 sec2017-8-5 20:7:14, thread_write_0: LockWrite wait...2017-8-5 20:7:14, thread_write_1: UnLockWrite2017-8-5 20:7:14, thread_write_0: LockWrite success and write count++ and sleep 1 sec2017-8-5 20:7:15, thread_read_0: LockRead success and read count = 2017-8-5 20:7:15, thread_write_0: UnLockWrite2017-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 sec5 and sleep 1 sec2017-8-5 20:7:16, thread_write_0: LockWrite wait...2017-8-5 20:7:16, thread_read_1: UnLockRead2017-8-5 20:7:16, thread_read_0: UnLockRead2017-8-5 20:7:16, thread_write_1: LockWrite success and write count++ and sleep 1 sec2017-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: UnLockWrite2017-8-5 20:7:17, thread_write_0: LockWrite success and write count++ and sleep 1 sec2017-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: UnLockWrite2017-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 sec2017-8-5 20:7:19, thread_read_0: LockRead success and read count = 8 and sleep 1 sec2017-8-5 20:7:19, thread_read_2: LockRead success and read count = 8 and sleep 1 sec2017-8-5 20:7:20, thread_write_1: LockWrite wait...2017-8-5 20:7:20, thread_read_1: UnLockRead2017-8-5 20:7:20, thread_read_0: UnLockRead2017-8-5 20:7:20, thread_read_2: UnLockRead2017-8-5 20:7:20, thread_write_1: LockWrite success and write count++ and sleep 1 sec2017-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: UnLockWrite2017-8-5 20:7:21, thread_write_0: LockWrite success and write count++ and sleep 1 sec2017-8-5 20:7:22, thread_write_0: UnLockWrite2017-8-5 20:7:22, thread_read_0: LockRead success and read count = 10 and sleep 1 sec2017-8-5 20:7:22, thread_read_1: LockRead success and read count = 10 and sleep 1 sec2017-8-5 20:7:22, thread_read_2: LockRead success and read count = 10 and sleep 1 sec2017-8-5 20:7:23, thread_read_0: UnLockRead2017-8-5 20:7:23, thread_read_1: UnLockRead2017-8-5 20:7:23, thread_read_2: UnLockRead2017-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 sec2017-8-5 20:7:25, thread_read_2: UnLockRead2017-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 sec2017-8-5 20:7:27, thread_read_2: UnLockRead#

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 程序
    +关注

    关注

    117

    文章

    3785

    浏览量

    81000
  • 系统
    +关注

    关注

    1

    文章

    1014

    浏览量

    21332
  • OpenHarmony
    +关注

    关注

    25

    文章

    3713

    浏览量

    16252
收藏 人收藏

    评论

    相关推荐

    基于OpenHarmony标准系统C++公共基础案例:ThreadPoll

    1、程序简介 该程序是基于OpenHarmony标准系统C++公共基础的线程池处理:Thr
    发表于 08-12 11:42

    基于OpenHarmony标准系统C++公共基础案例:Semaphore

    1、程序简介 该程序是基于OpenHarmony标准系统C++公共基础的线程处理:Semp
    发表于 08-14 16:38

    基于OpenHarmony标准系统C++公共基础案例:rwlock

    /samples/a25_utils_rwlock 2、基础知识 C++公共基础标准系统
    发表于 08-20 09:37

    基于OpenHarmony标准系统C++公共基础案例:SafeMap

    基础标准系统提供了一些常用的C++开发工具,包括: 文件、路径、字符串相关操作的能力增强接口 读写锁、信号量、定时器、线程增强及线程
    发表于 08-20 12:00

    基于OpenHarmony标准系统C++公共基础案例:SafeQueue

    /a27_utils_safequeue 2、基础知识 C++公共基础标准系统提供了一些常用的C+
    发表于 08-21 10:56

    基于OpenHarmony标准系统C++公共基础案例:SafeStack

    /a28_utils_safestack 2、基础知识 C++公共基础标准系统提供了一些常用的C+
    发表于 08-21 14:51

    基于OpenHarmony标准系统C++公共基础案例:SafeBlockQueue

    /samples/a29_utils_safeblockqueue 2、基础知识 C++公共基础标准系统提供了一些常用的
    发表于 08-22 10:52

    OpenHarmony C++公共基础应用案例:Thread

    程在第5秒时,关闭子线程运行。 创建1个子线程,每隔1秒打印当前运行次数。 2、基础知识 C++公共基础标准系统提供了一些常用的
    发表于 11-22 11:50

    OpenHarmony C++公共基础应用案例:Thread

    1、程序简介该程序是基于OpenHarmonyC++公共基础的线程处理:Thread。该应用案例已在
    的头像 发表于 11-23 08:22 910次阅读
    <b class='flag-5'>OpenHarmony</b> <b class='flag-5'>C++</b><b class='flag-5'>公共</b>基础<b class='flag-5'>类</b><b class='flag-5'>库</b>应用案例:Thread

    OpenHarmony C++公共基础应用案例:HelloWorld

    1、程序简介该程序是基于OpenHarmonyC++公共基础的简单案例:HelloWorld。该应用案例已在
    的头像 发表于 11-23 08:22 695次阅读
    <b class='flag-5'>OpenHarmony</b> <b class='flag-5'>C++</b><b class='flag-5'>公共</b>基础<b class='flag-5'>类</b><b class='flag-5'>库</b>应用案例:HelloWorld

    OpenHarmony标准系统C++公共基础案例:HelloWorld

    1、程序简介该程序是基于凌蒙派OpenHarmony-v3.2.1标准系统C++公共基础的简
    的头像 发表于 08-13 08:23 511次阅读
    <b class='flag-5'>OpenHarmony</b><b class='flag-5'>标准系统</b><b class='flag-5'>C++</b><b class='flag-5'>公共</b>基础<b class='flag-5'>类</b><b class='flag-5'>库</b>案例:HelloWorld

    基于OpenHarmony标准系统C++公共基础案例:SafeBlockQueue

    1、程序简介该程序是基于OpenHarmonyC++公共基础的读写锁:SafeBlockQueue。线程安全阻塞队列SafeBlock
    的头像 发表于 08-30 12:41 294次阅读
    基于<b class='flag-5'>OpenHarmony</b><b class='flag-5'>标准系统</b>的<b class='flag-5'>C++</b><b class='flag-5'>公共</b>基础<b class='flag-5'>类</b><b class='flag-5'>库</b>案例:SafeBlockQueue

    基于OpenHarmony标准系统C++公共基础案例:SafeStack

    1、程序简介该程序是基于OpenHarmonyC++公共基础的线程安全队列:SafeQueue。线程安全队列,是在dequeue的基础
    的头像 发表于 08-30 12:41 318次阅读
    基于<b class='flag-5'>OpenHarmony</b><b class='flag-5'>标准系统</b>的<b class='flag-5'>C++</b><b class='flag-5'>公共</b>基础<b class='flag-5'>类</b><b class='flag-5'>库</b>案例:SafeStack

    基于OpenHarmony标准系统C++公共基础案例:SafeQueue

    1、程序简介该程序是基于OpenHarmonyC++公共基础的线程安全队列:SafeQueue。线程安全队列,是在dequeue的基础
    的头像 发表于 08-30 12:41 288次阅读
    基于<b class='flag-5'>OpenHarmony</b><b class='flag-5'>标准系统</b>的<b class='flag-5'>C++</b><b class='flag-5'>公共</b>基础<b class='flag-5'>类</b><b class='flag-5'>库</b>案例:SafeQueue

    基于OpenHarmony标准系统C++公共基础案例:SafeMap

    1、程序简介该程序是基于OpenHarmonyC++公共基础的安全关联容器:SafeMap。Ope
    的头像 发表于 08-30 12:42 344次阅读
    基于<b class='flag-5'>OpenHarmony</b><b class='flag-5'>标准系统</b>的<b class='flag-5'>C++</b><b class='flag-5'>公共</b>基础<b class='flag-5'>类</b><b class='flag-5'>库</b>案例:SafeMap