基于OpenHarmony标准系统的C++公共基础类库案例:SafeMap 原创
1、程序简介
该程序是基于OpenHarmony的C++公共基础类库的安全关联容器:SafeMap。
OpenHarmony提供了一个线程安全的map实现。SafeMap在STL map基础上封装互斥锁,以确保对map的操作安全。
本案例主要完成如下工作:
- 创建1个子线程,负责每秒调用EnsureInsert()插入元素;
- 创建1个子线程,负责每秒调用Insert()插入元素;
- 创建1个子线程,负责每秒调用Erase()删除元素;
- 创建1个子线程,负责每秒调用FindOldAndSetNew()替换元素的值;
- 主线程等待上述线程结束,Iterate()和Find()查看所有元素;
- 主线程等待上述线程结束,清空SafeMap,并调用IsEmpty()查看是否确实是空。
该案例已在凌蒙派-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、SafeMap头文件
C++公共基础类库的safemap头文件在://commonlibrary/c_utils/base/include/safe_map.h
可在源代码中添加如下:
#include <safe_map.h>
2.3、OHOS::SafeMap接口说明
2.3.1、SafeMap
构造函数。
SafeMap();
SafeMap(const SafeMap& rhs);
参数说明:
参数名称 | 类型 | 参数说明 |
---|---|---|
rhs | SafeMap | 复制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);
参数说明:
参数名称 | 类型 | 参数说明 |
---|---|---|
key | K | 需要插入元素的关键字 |
value | V | 需要插入元素的值 |
2.3.5、Erase
删除map中键为key的键值对。
void Erase(const K& key);
参数说明:
参数名称 | 类型 | 参数说明 |
---|---|---|
key | K | 需要删除元素的关键字 |
2.3.6、Find
在map中查找元素。
bool Find(const K& key, V& value);
参数说明:
参数名称 | 类型 | 参数说明 |
---|---|---|
key | K | 需要查找元素的关键字 |
value | V | 需要查找元素的值 |
返回值说明:
类型 | 返回值说明 |
---|---|
bool | true表示成功,false表示失败 |
2.3.7、FindOldAndSetNew
在map中查找元素并将key对应的oldValue
替换为newValue
。
bool FindOldAndSetNew(const K& key, V& oldValue, const V& newValue);
参数说明:
参数名称 | 类型 | 参数说明 |
---|---|---|
key | K | 需要替换元素的关键字 |
oldValue | V | 需要替换元素的原始值 |
newValue | V | 需要替换元素的新值 |
返回值说明:
类型 | 返回值说明 |
---|---|
bool | true表示成功,false表示失败 |
2.3.8、Insert
在map中插入新元素。
bool Insert(const K& key, const V& value);
参数说明:
参数名称 | 类型 | 参数说明 |
---|---|---|
key | K | 需要插入元素的关键字 |
value | V | 需要插入元素的原始值 |
返回值说明:
类型 | 返回值说明 |
---|---|
bool | true表示成功,false表示失败 |
2.3.9、IsEmpty
判断map是否为空。
bool IsEmpty();
返回值说明:
类型 | 返回值说明 |
---|---|
bool | true表示空,false表示非空 |
2.3.10、Iterate
遍历map中的元素。
bool Iterate(const SafeMapCallBack& callback);
参数说明:
参数名称 | 类型 | 参数说明 |
---|---|---|
callback | SafeMapCallBack | 遍历执行函数 |
2.3.11、operator=
SafeMap赋值。
SafeMap& operator=(const SafeMap& rhs);
参数说明:
参数名称 | 类型 | 参数说明 |
---|---|---|
rhs | SafeMap& | 被赋值的SafeMap类对象 |
返回值说明:
类型 | 返回值说明 |
---|---|
SafeMap | 赋值的SafeMap类对象 |
2.3.12、operator[]
SafeMap索引。
V& operator[](const K& key);
参数说明:
参数名称 | 类型 | 参数说明 |
---|---|---|
key | K& | 元素的关键字 |
返回值说明:
类型 | 返回值说明 |
---|---|
V& | 返回元素的值 |
2.3.13、Size
获取map的size大小。
int Size();
返回值说明:
类型 | 返回值说明 |
---|---|
int | map的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-build
sudo apt install generate-ninja
# 规范化BUILD.gn
gn format BUILD.gn
3.4、创建源代码
3.4.1、创建SafeMap
#include <safe_map.h> // SafeMap的头文件
// 定义SafeMap变量
static OHOS::SafeMap<int, string> 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_safemap
Thread_EnsureInsert: insert successful and key = 1 and value = aaa
Thread_Erase: Erase successful and key = Thread_FindOldAndSetNew: FindOldAndSetNew successful and key = 1 and old_value = aaa and new_value = abc
101
Thread_Insert: insert successful and key = 101 and value = 111
Thread_EnsureInsert: insert successful and key = Thread_FindOldAndSetNew: FindOldAndSetNew successful and key = 2 and old_value = bbb and new_value = bcd
Thread_Insert: insert successful and key = 102 and value = 2222 and value = bbb
Thread_Erase: Erase successful and key = 102
Thread_EnsureInsert: insert successful and key = 3 and value = ccc
Thread_FindOldAndSetNew: FindOldAndSetNew successful and key = 3 and old_value = ccc and new_value = cde
Thread_Insert: insert successful and key = 103 and value = 333
Thread_Erase: Erase successful and key = 103
Thread_EnsureInsert: insert successful and key = 4 and value = ddd
Thread_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 = 104
Thread_EnsureInsert: insert successful and key = 5 and value = eee
Thread_Insert: insert successful and key = 105 and value = 555
Thread_FindOldAndSetNew: FindOldAndSetNew successful and key = 5 and old_value = eee and new_value = efg
Thread_Erase: Erase successful and key = 105
Thread_EnsureInsert: insert successful and key = 6 and value = fff
Thread_FindOldAndSetNew: FindOldAndSetNew successful and key = 6 and old_value = fff and new_value = fghThread_Insert: insert successful and key =
106 and value = 666
Thread_Erase: Erase successful and key = 106
Thread_EnsureInsert: insert successful and key = 7 and value = ggg
Thread_FindOldAndSetNew: FindOldAndSetNew successful and key = 7 and old_value = ggg and new_value = ghi
Thread_Erase: Erase successful and key = 107
Thread_Insert: insert successful and key = 107 and value = 777
Thread_EnsureInsert: insert successful and key = 8 and value = hhh
Thread_FindOldAndSetNew: FindOldAndSetNew successful and key = 8 and old_value = hhh and new_value = hij
Thread_Erase: Erase successful and key = 108
Thread_Insert: insert successful and key = 108 and value = 888
Thread_EnsureInsert: insert successful and key = 9 and value = iii
Thread_FindOldAndSetNew: FindOldAndSetNew successful and key = 9 and old_value = iii and new_value = ijk
Thread_Erase: Erase successful and key = 109
Thread_Insert: insert successful and key = 109 and value = 999
Thread_EnsureInsert: insert successful and key = 10 and value = jjj
Thread_FindOldAndSetNew: FindOldAndSetNew successful and key = 10 and old_value = jjj and new_value = jkl
Thread_Erase: Erase successful and key = 110
Thread_Insert: insert successful and key = 110 and value = 000
Threads Stop
SafeMap Iterate:
key = 1, value = abc
key = 2, value = bcd
key = 3, value = cde
key = 4, value = def
key = 5, value = efg
key = 6, value = fgh
key = 7, value = ghi
key = 8, value = hij
key = 9, value = ijk
key = 10, value = jkl
key = 108, value = 888
key = 109, value = 999
key = 110, value = 000
SafeMap Find:
key = 1, value = abc
key = 2, value = bcd
key = 3, value = cde
key = 4, value = def
key = 5, value = efg
key = 6, value = fgh
key = 7, value = ghi
key = 8, value = hij
key = 9, value = ijk
key = 10, value = jkl
key = 108, value = 888
key = 109, value = 999
key = 110, value = 000
SafeMap Clear
SafeMap IsEmpty: 1
#