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

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

3天内不再提示

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

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

1、程序简介

该程序是基于OpenHarmonyC++公共基础类库的安全关联容器:SafeMap。

OpenHarmony提供了一个线程安全的map实现。SafeMap在STL map基础上封装互斥锁,以确保对map的操作安全。

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

创建1个子线程,负责每秒调用EnsureInsert()插入元素;

创建1个子线程,负责每秒调用Insert()插入元素;

创建1个子线程,负责每秒调用Erase()删除元素;

创建1个子线程,负责每秒调用FindOldAndSetNew()替换元素的值;

主线程等待上述线程结束,Iterate()和Find()查看所有元素;

主线程等待上述线程结束,清空SafeMap,并调用IsEmpty()查看是否确实是空。

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、SafeMap头文件

C++公共基础类库的safemap头文件在://commonlibrary/c_utils/base/include/safe_map.h

可在源代码中添加如下:

#include

2.3、OHOS::SafeMap接口说明

2.3.1、SafeMap

构造函数。

SafeMap();SafeMap(const SafeMap& rhs);

参数说明:

参数名称类型参数说明
rhsSafeMap复制SafeMap的类对象

2.3.2、~SafeMap

析构函数。

~SafeMap();

2.3.3、Clear

删除map中存储的所有键值对。

void Clear();

2.3.4、EnsureInsert

在map中插入元素。

void EnsureInsert(const K& key, const V& value);

参数说明:

参数名称类型参数说明
keyK需要插入元素的关键字
valueV需要插入元素的值

2.3.5、Erase

删除map中键为key的键值对。

void Erase(const K& key);

参数说明:

参数名称类型参数说明
keyK需要删除元素的关键字

2.3.6、Find

在map中查找元素。

bool Find(const K& key, V& value);

参数说明:

参数名称类型参数说明
keyK需要查找元素的关键字
valueV需要查找元素的值

返回值说明:

类型返回值说明
booltrue表示成功,false表示失败

2.3.7、FindOldAndSetNew

在map中查找元素并将key对应的oldValue替换为newValue。

boolFindOldAndSetNew(constK&key,V&oldValue,constV&newValue);

参数说明:

参数名称类型参数说明
keyK需要替换元素的关键字
oldValueV需要替换元素的原始值
newValueV需要替换元素的新值

返回值说明:

类型返回值说明
booltrue表示成功,false表示失败

2.3.8、Insert

在map中插入新元素。

bool Insert(const K& key, const V& value);

参数说明:

参数名称类型参数说明
keyK需要插入元素的关键字
valueV需要插入元素的原始值

返回值说明:

类型返回值说明
booltrue表示成功,false表示失败

2.3.9、IsEmpty

判断map是否为空。

bool IsEmpty();

返回值说明:

类型返回值说明
booltrue表示空,false表示非空

2.3.10、Iterate

遍历map中的元素。

bool Iterate(const SafeMapCallBack& callback);

参数说明:

参数名称类型参数说明
callbackSafeMapCallBack遍历执行函数

2.3.11、operator=

SafeMap赋值。

SafeMap& operator=(const SafeMap& rhs);

参数说明:

参数名称类型参数说明
rhsSafeMap&被赋值的SafeMap类对象

返回值说明:

类型返回值说明
SafeMap赋值的SafeMap类对象

2.3.12、operator[]

SafeMap索引

V& operator[](const K& key);

参数说明:

参数名称类型参数说明
keyK&元素的关键字

返回值说明:

类型返回值说明
V&返回元素的值

2.3.13、Size

获取map的size大小。

int Size();

返回值说明:

类型返回值说明
intmap的size大小

3、程序解析

3.1、创建编译引导

在上一级目录BUILD.gn文件添加一行编译引导语句。

import("//build/ohos.gni")
group("samples") { deps = [ "a26_utils_safemap:utils_safemap", # 添加该行 ]}

"a26_utils_safemap:utils_safemap",该行语句表示引入 参与编译。

3.2、创建编译项目

创建a26_utils_safemap目录,并添加如下文件:

a26_utils_safemap├── utils_safemap_sample.cpp # .cpp源代码├── BUILD.gn # GN文件

3.3、创建BUILD.gn

编辑BUILD.gn文件。

import("//build/ohos.gni")ohos_executable("utils_safemap") { sources = [ "utils_safemap_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、创建SafeMap

#include // SafeMap的头文件
// 定义SafeMap变量static OHOS::SafeMap m_safemap;

3.4.2、创建线程池并设置

int main(int argc, char **argv){ OHOS::ThreadPool threads("name_rwlock_threads"); string str_name; ...... threads.SetMaxTaskNum(128); threads.Start(4); ......}

3.4.3、启动4个子线程,并等待结束

调用AddTask()添加子线程,并调用Stop()等待所有子进程结束。

// 开启子线程,使用EnsureInsert插入元素str_name = "Thread_EnsureInsert";auto task_ensure_insert = std::bind(map_ensure_insert, str_name);threads.AddTask(task_ensure_insert);
// 开启子线程,使用Insert插入元素str_name = "Thread_Insert";auto task_insert = std::bind(map_insert, str_name);threads.AddTask(task_insert);
// 开启子线程,使用erase删除元素str_name = "Thread_Erase";auto task_erase = std::bind(map_erase, str_name);threads.AddTask(task_erase);
// 开启子线程,使用FindOldAndSetNew替换元素的值str_name = "Thread_FindOldAndSetNew";auto task_findold_and_setnew = std::bind(map_findold_and_setnew, str_name);threads.AddTask(task_findold_and_setnew);
// 设置结束,并等待结束threads.Stop();cout << "Threads Stop" << endl;

3.4.4、编写SafeMap.EnsureInsert()插入元素

void map_ensure_insert(const string& name){ int key = 0; string value = ""; for (int i = 0; i < (sizeof(m_map1_insert) / sizeof(struct MapInfo)); i++) { key = m_map1_insert[i].key; value = m_map1_insert[i].str; m_safemap.EnsureInsert(key, value); cout << name << ": insert successful and key = " << key << " and value = " << value << endl; sleep(1); }}

3.4.5、编写SafeMap.Insert()插入元素

void map_insert(const string& name){ int key = 0; string value = ""; for (int i = 0; i < (sizeof(m_map2_insert) / sizeof(struct MapInfo)); i++) { key = m_map2_insert[i].key; value = m_map2_insert[i].str; if (m_safemap.Insert(key, value) == false) { cout << name << ": insert failed and key = " << to_string(key) << " and value = " << value << endl; } else { cout << name << ": insert successful and key = " << to_string(key) << " and value = " << value << endl; } sleep(1); }}

3.4.6、编写SafeMap.Erase()删除元素

void map_erase(const string& name){ int key = 0; string value = ""; for (int i = 0; i < (sizeof(m_map2_insert) / sizeof(struct MapInfo)); i++) { key = m_map2_insert[i].key; m_safemap.Erase(key); cout << name << ": Erase successful and key = " << to_string(key) << endl; sleep(1); }}

3.4.7、编写SafeMap.FindOldAndSetNew()替换元素的值

void map_findold_and_setnew(const string& name){ int key = 0; string old_value = ""; string new_value = ""; for (int i = 0; i < (sizeof(m_map1_insert) / sizeof(struct MapInfo)); i++) { key = m_map1_reset[i].key; old_value = ""; new_value = m_map1_reset[i].str; if (m_safemap.FindOldAndSetNew(key, old_value, new_value) == false) { cout << name << ": FindOldAndSetNew failed and key = " << to_string(key) << " and old_value = " << old_value << endl; } else { cout << name << ": FindOldAndSetNew successful and key = " << to_string(key) << " and old_value = " << old_value << " and new_value = " << new_value << endl; } sleep(1); }}

3.4.8、编写枚举所有元素

主要分为如下两种方法:

(1)调用SafeMap.Iterate()

void map_iterate_print(const int key, string& value){ cout << "key = " << to_string(key) << ", value = " << value << endl;}
int main(int argc, char *argv[]){ ...... cout << "SafeMap Iterate: " << endl; m_safemap.Iterate(map_iterate_print); ......}

(2)调用SafeMap.Find()

void map_find_print(){ int key = 0; string value = ""; for (int i = 0; i < (sizeof(m_map1_insert) / sizeof(struct MapInfo)); i++) { key = m_map1_insert[i].key; value = ""; if (m_safemap.Find(key, value)) { cout << "key = " << to_string(key) << ", value = " << value << endl; } } for (int i = 0; i < (sizeof(m_map2_insert) / sizeof(struct MapInfo)); i++) { key = m_map2_insert[i].key; value = ""; if (m_safemap.Find(key, value)) { cout << "key = " << to_string(key) << ", value = " << value << endl; } }}

3.4.9、清空SafeMap

int main(int argc, char *argv[]){ ...... cout << "SafeMap Clear" << endl; m_safemap.Clear(); cout << "SafeMap IsEmpty: " << m_safemap.IsEmpty() << endl; ......}

4、编译步骤

进入OpenHarmony编译环境,运行命令:

hb build -f

5、运行结果

# utils_safemapThread_EnsureInsert: insert successful and key = 1 and value = aaaThread_Erase: Erase successful and key = Thread_FindOldAndSetNew: FindOldAndSetNew successful and key = 1 and old_value = aaa and new_value = abc101Thread_Insert: insert successful and key = 101 and value = 111Thread_EnsureInsert: insert successful and key = Thread_FindOldAndSetNew: FindOldAndSetNew successful and key = 2 and old_value = bbb and new_value = bcdThread_Insert: insert successful and key = 102 and value = 2222 and value = bbb
Thread_Erase: Erase successful and key = 102Thread_EnsureInsert: insert successful and key = 3 and value = cccThread_FindOldAndSetNew: FindOldAndSetNew successful and key = 3 and old_value = ccc and new_value = cdeThread_Insert: insert successful and key = 103 and value = 333Thread_Erase: Erase successful and key = 103Thread_EnsureInsert: insert successful and key = 4 and value = dddThread_Insert: insert successful and key = 104Thread_FindOldAndSetNew and value = : FindOldAndSetNew successful and key = 4444 and old_value = ddd and new_value = def
Thread_Erase: Erase successful and key = 104Thread_EnsureInsert: insert successful and key = 5 and value = eeeThread_Insert: insert successful and key = 105 and value = 555Thread_FindOldAndSetNew: FindOldAndSetNew successful and key = 5 and old_value = eee and new_value = efgThread_Erase: Erase successful and key = 105Thread_EnsureInsert: insert successful and key = 6 and value = fffThread_FindOldAndSetNew: FindOldAndSetNew successful and key = 6 and old_value = fff and new_value = fghThread_Insert: insert successful and key = 106 and value = 666Thread_Erase: Erase successful and key = 106Thread_EnsureInsert: insert successful and key = 7 and value = gggThread_FindOldAndSetNew: FindOldAndSetNew successful and key = 7 and old_value = ggg and new_value = ghiThread_Erase: Erase successful and key = 107Thread_Insert: insert successful and key = 107 and value = 777Thread_EnsureInsert: insert successful and key = 8 and value = hhhThread_FindOldAndSetNew: FindOldAndSetNew successful and key = 8 and old_value = hhh and new_value = hijThread_Erase: Erase successful and key = 108Thread_Insert: insert successful and key = 108 and value = 888Thread_EnsureInsert: insert successful and key = 9 and value = iiiThread_FindOldAndSetNew: FindOldAndSetNew successful and key = 9 and old_value = iii and new_value = ijkThread_Erase: Erase successful and key = 109Thread_Insert: insert successful and key = 109 and value = 999Thread_EnsureInsert: insert successful and key = 10 and value = jjjThread_FindOldAndSetNew: FindOldAndSetNew successful and key = 10 and old_value = jjj and new_value = jklThread_Erase: Erase successful and key = 110Thread_Insert: insert successful and key = 110 and value = 000Threads StopSafeMap Iterate:key = 1, value = abckey = 2, value = bcdkey = 3, value = cdekey = 4, value = defkey = 5, value = efgkey = 6, value = fghkey = 7, value = ghikey = 8, value = hijkey = 9, value = ijkkey = 10, value = jklkey = 108, value = 888key = 109, value = 999key = 110, value = 000SafeMap Find:key = 1, value = abckey = 2, value = bcdkey = 3, value = cdekey = 4, value = defkey = 5, value = efgkey = 6, value = fghkey = 7, value = ghikey = 8, value = hijkey = 9, value = ijkkey = 10, value = jklkey = 108, value = 888key = 109, value = 999key = 110, value = 000SafeMap ClearSafeMap IsEmpty: 1#

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

    关注

    0

    文章

    49

    浏览量

    15145
  • Safe
    +关注

    关注

    0

    文章

    6

    浏览量

    7249
  • OpenHarmony
    +关注

    关注

    25

    文章

    3723

    浏览量

    16329
收藏 人收藏

    评论

    相关推荐

    基于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

    1、程序简介 该程序是基于OpenHarmonyC++公共基础的安全关联容器:SafeMap
    发表于 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 935次阅读
    <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 705次阅读
    <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 527次阅读
    <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 314次阅读
    基于<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 334次阅读
    基于<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 313次阅读
    基于<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++公共基础案例:rwlock

    1、程序简介该程序是基于OpenHarmonyC++公共基础的读写锁:rwlock。本案例主要完成如下工作:创建3个读线程,每个读线程
    的头像 发表于 08-30 12:42 382次阅读
    基于<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>案例:rwlock