1、程序簡(jiǎn)介
該程序是基于OpenHarmony的C++公共基礎(chǔ)類(lèi)庫(kù)的安全關(guān)聯(lián)容器:SafeMap。
OpenHarmony提供了一個(gè)線程安全的map實(shí)現(xiàn)。SafeMap在STL map基礎(chǔ)上封裝互斥鎖,以確保對(duì)map的操作安全。
本案例主要完成如下工作:
創(chuàng)建1個(gè)子線程,負(fù)責(zé)每秒調(diào)用EnsureInsert()插入元素;
創(chuàng)建1個(gè)子線程,負(fù)責(zé)每秒調(diào)用Insert()插入元素;
創(chuàng)建1個(gè)子線程,負(fù)責(zé)每秒調(diào)用Erase()刪除元素;
創(chuàng)建1個(gè)子線程,負(fù)責(zé)每秒調(diào)用FindOldAndSetNew()替換元素的值;
主線程等待上述線程結(jié)束,Iterate()和Find()查看所有元素;
主線程等待上述線程結(jié)束,清空SafeMap,并調(diào)用IsEmpty()查看是否確實(shí)是空。
2、基礎(chǔ)知識(shí)
C++公共基礎(chǔ)類(lèi)庫(kù)為標(biāo)準(zhǔn)系統(tǒng)提供了一些常用的C++開(kāi)發(fā)工具類(lèi),包括:
文件、路徑、字符串相關(guān)操作的能力增強(qiáng)接口
讀寫(xiě)鎖、信號(hào)量、定時(shí)器、線程增強(qiáng)及線程池等接口
安全數(shù)據(jù)容器、數(shù)據(jù)序列化等接口
各子系統(tǒng)的錯(cuò)誤碼相關(guān)定義
2.1、添加C++公共基礎(chǔ)類(lèi)庫(kù)依賴(lài)
修改需調(diào)用模塊的BUILD.gn,在external_deps或deps中添加如下:
ohos_shared_library("xxxxx") { ... external_deps = [ ... # 動(dòng)態(tài)庫(kù)依賴(lài)(可選) "c_utils:utils", # 靜態(tài)庫(kù)依賴(lài)(可選) "c_utils:utilsbase", # Rust動(dòng)態(tài)庫(kù)依賴(lài)(可選) "c_utils:utils_rust", ] ...}
一般而言,我們只需要填寫(xiě)"c_utils:utils"即可。
2.2、SafeMap頭文件
C++公共基礎(chǔ)類(lèi)庫(kù)的safemap頭文件在://commonlibrary/c_utils/base/include/safe_map.h
可在源代碼中添加如下:
#include
2.3、OHOS::SafeMap接口說(shuō)明
2.3.1、SafeMap
構(gòu)造函數(shù)。
SafeMap();SafeMap(const SafeMap& rhs);
參數(shù)說(shuō)明:
參數(shù)名稱(chēng) | 類(lèi)型 | 參數(shù)說(shuō)明 |
---|---|---|
rhs | SafeMap | 復(fù)制SafeMap的類(lèi)對(duì)象 |
2.3.2、~SafeMap
析構(gòu)函數(shù)。
~SafeMap();
2.3.3、Clear
刪除map中存儲(chǔ)的所有鍵值對(duì)。
void Clear();
2.3.4、EnsureInsert
在map中插入元素。
void EnsureInsert(const K& key, const V& value);
參數(shù)說(shuō)明:
參數(shù)名稱(chēng) | 類(lèi)型 | 參數(shù)說(shuō)明 |
---|---|---|
key | K | 需要插入元素的關(guān)鍵字 |
value | V | 需要插入元素的值 |
2.3.5、Erase
刪除map中鍵為key的鍵值對(duì)。
void Erase(const K& key);
參數(shù)說(shuō)明:
參數(shù)名稱(chēng) | 類(lèi)型 | 參數(shù)說(shuō)明 |
---|---|---|
key | K | 需要?jiǎng)h除元素的關(guān)鍵字 |
2.3.6、Find
在map中查找元素。
bool Find(const K& key, V& value);
參數(shù)說(shuō)明:
參數(shù)名稱(chēng) | 類(lèi)型 | 參數(shù)說(shuō)明 |
---|---|---|
key | K | 需要查找元素的關(guān)鍵字 |
value | V | 需要查找元素的值 |
返回值說(shuō)明:
類(lèi)型 | 返回值說(shuō)明 |
---|---|
bool | true表示成功,false表示失敗 |
2.3.7、FindOldAndSetNew
在map中查找元素并將key對(duì)應(yīng)的oldValue替換為newValue。
boolFindOldAndSetNew(constK&key,V&oldValue,constV&newValue);
參數(shù)說(shuō)明:
參數(shù)名稱(chēng) | 類(lèi)型 | 參數(shù)說(shuō)明 |
---|---|---|
key | K | 需要替換元素的關(guān)鍵字 |
oldValue | V | 需要替換元素的原始值 |
newValue | V | 需要替換元素的新值 |
返回值說(shuō)明:
類(lèi)型 | 返回值說(shuō)明 |
---|---|
bool | true表示成功,false表示失敗 |
2.3.8、Insert
在map中插入新元素。
bool Insert(const K& key, const V& value);
參數(shù)說(shuō)明:
參數(shù)名稱(chēng) | 類(lèi)型 | 參數(shù)說(shuō)明 |
---|---|---|
key | K | 需要插入元素的關(guān)鍵字 |
value | V | 需要插入元素的原始值 |
返回值說(shuō)明:
類(lèi)型 | 返回值說(shuō)明 |
---|---|
bool | true表示成功,false表示失敗 |
2.3.9、IsEmpty
判斷map是否為空。
bool IsEmpty();
返回值說(shuō)明:
類(lèi)型 | 返回值說(shuō)明 |
---|---|
bool | true表示空,false表示非空 |
2.3.10、Iterate
遍歷map中的元素。
bool Iterate(const SafeMapCallBack& callback);
參數(shù)說(shuō)明:
參數(shù)名稱(chēng) | 類(lèi)型 | 參數(shù)說(shuō)明 |
---|---|---|
callback | SafeMapCallBack | 遍歷執(zhí)行函數(shù) |
2.3.11、operator=
SafeMap賦值。
SafeMap& operator=(const SafeMap& rhs);
參數(shù)說(shuō)明:
參數(shù)名稱(chēng) | 類(lèi)型 | 參數(shù)說(shuō)明 |
---|---|---|
rhs | SafeMap& | 被賦值的SafeMap類(lèi)對(duì)象 |
返回值說(shuō)明:
類(lèi)型 | 返回值說(shuō)明 |
---|---|
SafeMap | 賦值的SafeMap類(lèi)對(duì)象 |
2.3.12、operator[]
SafeMap索引。
V& operator[](const K& key);
參數(shù)說(shuō)明:
參數(shù)名稱(chēng) | 類(lèi)型 | 參數(shù)說(shuō)明 |
---|---|---|
key | K& | 元素的關(guān)鍵字 |
返回值說(shuō)明:
類(lèi)型 | 返回值說(shuō)明 |
---|---|
V& | 返回元素的值 |
2.3.13、Size
獲取map的size大小。
int Size();
返回值說(shuō)明:
類(lèi)型 | 返回值說(shuō)明 |
---|---|
int | map的size大小 |
3、程序解析
3.1、創(chuàng)建編譯引導(dǎo)
在上一級(jí)目錄BUILD.gn文件添加一行編譯引導(dǎo)語(yǔ)句。
import("http://build/ohos.gni")
group("samples") { deps = [ "a26_utils_safemap:utils_safemap", # 添加該行 ]}
"a26_utils_safemap:utils_safemap",該行語(yǔ)句表示引入 參與編譯。
3.2、創(chuàng)建編譯項(xiàng)目
創(chuàng)建a26_utils_safemap目錄,并添加如下文件:
a26_utils_safemap├── utils_safemap_sample.cpp # .cpp源代碼├── BUILD.gn # GN文件
3.3、創(chuàng)建BUILD.gn
編輯BUILD.gn文件。
import("http://build/ohos.gni")ohos_executable("utils_safemap") { sources = [ "utils_safemap_sample.cpp" ] include_dirs = [ "http://commonlibrary/c_utils/base/include", "http://commonlibrary/c_utils/base:utils", "http://third_party/googletest:gtest_main", "http://third_party/googletest/googletest/include" ] external_deps = [ "c_utils:utils" ] part_name = "product_rk3568" install_enable = true}
注意:
(1)BUILD.gn中所有的TAB鍵必須轉(zhuǎn)化為空格,否則會(huì)報(bào)錯(cuò)。如果自己不知道如何規(guī)范化,可以:
# 安裝gn工具sudo apt-get install ninja-buildsudo apt install generate-ninja# 規(guī)范化BUILD.gngn format BUILD.gn
3.4、創(chuàng)建源代碼
3.4.1、創(chuàng)建SafeMap
#include // SafeMap的頭文件
// 定義SafeMap變量static OHOS::SafeMap
3.4.2、創(chuàng)建線程池并設(shè)置
int main(int argc, char **argv){ OHOS::ThreadPool threads("name_rwlock_threads"); string str_name; ...... threads.SetMaxTaskNum(128); threads.Start(4); ......}
3.4.3、啟動(dòng)4個(gè)子線程,并等待結(jié)束
調(diào)用AddTask()添加子線程,并調(diào)用Stop()等待所有子進(jìn)程結(jié)束。
// 開(kāi)啟子線程,使用EnsureInsert插入元素str_name = "Thread_EnsureInsert";auto task_ensure_insert = std::bind(map_ensure_insert, str_name);threads.AddTask(task_ensure_insert);
// 開(kāi)啟子線程,使用Insert插入元素str_name = "Thread_Insert";auto task_insert = std::bind(map_insert, str_name);threads.AddTask(task_insert);
// 開(kāi)啟子線程,使用erase刪除元素str_name = "Thread_Erase";auto task_erase = std::bind(map_erase, str_name);threads.AddTask(task_erase);
// 開(kāi)啟子線程,使用FindOldAndSetNew替換元素的值str_name = "Thread_FindOldAndSetNew";auto task_findold_and_setnew = std::bind(map_findold_and_setnew, str_name);threads.AddTask(task_findold_and_setnew);
// 設(shè)置結(jié)束,并等待結(jié)束threads.Stop();cout << "Threads Stop" << endl;
3.4.4、編寫(xiě)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、編寫(xiě)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、編寫(xiě)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、編寫(xiě)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、編寫(xiě)枚舉所有元素
主要分為如下兩種方法:
(1)調(diào)用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)調(diào)用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、編譯步驟
進(jìn)入OpenHarmony編譯環(huán)境,運(yùn)行命令:
hb build -f
5、運(yùn)行結(jié)果
# 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
+關(guān)注
關(guān)注
0文章
49瀏覽量
15405 -
Safe
+關(guān)注
關(guān)注
0文章
6瀏覽量
7317 -
OpenHarmony
+關(guān)注
關(guān)注
26文章
3820瀏覽量
18113
發(fā)布評(píng)論請(qǐng)先 登錄
基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)的C++公共基礎(chǔ)類(lèi)庫(kù)案例:ThreadPoll
基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)的C++公共基礎(chǔ)類(lèi)庫(kù)案例:Semaphore
基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)的C++公共基礎(chǔ)類(lèi)庫(kù)案例:rwlock
基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)的C++公共基礎(chǔ)類(lèi)庫(kù)案例:SafeMap
基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)的C++公共基礎(chǔ)類(lèi)庫(kù)案例:SafeQueue
基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)的C++公共基礎(chǔ)類(lèi)庫(kù)案例:SafeStack
OpenHarmony C++公共基礎(chǔ)類(lèi)庫(kù)應(yīng)用案例:Thread

OpenHarmony C++公共基礎(chǔ)類(lèi)庫(kù)應(yīng)用案例:HelloWorld

OpenHarmony標(biāo)準(zhǔn)系統(tǒng)C++公共基礎(chǔ)類(lèi)庫(kù)案例:HelloWorld

基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)的C++公共基礎(chǔ)類(lèi)庫(kù)案例:SafeBlockQueue

基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)的C++公共基礎(chǔ)類(lèi)庫(kù)案例:SafeStack

基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)的C++公共基礎(chǔ)類(lèi)庫(kù)案例:SafeQueue

基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)的C++公共基礎(chǔ)類(lèi)庫(kù)案例:rwlock

基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)的C++公共基礎(chǔ)類(lèi)庫(kù)案例:Semaphore

基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)的C++公共基礎(chǔ)類(lèi)庫(kù)案例:ThreadPoll

評(píng)論