1、程序簡介
該程序是基于OpenHarmony的C++公共基礎類庫的讀寫鎖:rwlock。
本案例主要完成如下工作:
創(chuàng)建3個讀線程,每個讀線程循環(huán)5次,每次循環(huán)獲取讀鎖,將公共資源變量打印,睡眠1秒,然后釋放讀鎖,最后再睡眠1秒。
創(chuàng)建3個寫線程,每個寫線程循環(huán)5次,每次循環(huán)獲取寫鎖,將公共資源變量打印,睡眠1秒,然后釋放讀鎖,最后再睡眠1秒。
2、基礎知識
C++公共基礎類庫為標準系統(tǒng)提供了一些常用的C++開發(fā)工具類,包括:
文件、路徑、字符串相關操作的能力增強接口
安全數據容器、數據序列化等接口
各子系統(tǒng)的錯誤碼相關定義
2.1、添加C++公共基礎類庫依賴
修改需調用模塊的BUILD.gn,在external_deps或deps中添加如下:
ohos_shared_library("xxxxx") { ... external_deps = [ ... # 動態(tài)庫依賴(可選) "c_utils:utils", # 靜態(tài)庫依賴(可選) "c_utils:utilsbase", # Rust動態(tài)庫依賴(可選) "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);
參數說明:
參數名稱 | 類型 | 參數說明 |
---|---|---|
writeFirst | bool | 是否優(yōu)先寫模式 |
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)
參數說明:
參數名稱 | 類型 | 參數說明 |
---|---|---|
rwLockable | RWLockable | template |
2.4.2、~UniqueWriteGuard
析構函數。
~UniqueWriteGuard();
2.5、OHOS::UniqueReadGuard接口說明
2.5.1、UniqueReadGuard
構造函數。
UniqueReadGuard(RWLockable &rwLockable)
參數說明:
參數名稱 | 類型 | 參數說明 |
---|---|---|
rwLockable | RWLockable | template |
2.5.2、~UniqueReadGuard
析構函數。
~UniqueReadGuard();
3、程序解析
3.1、創(chuàng)建編譯引導
在上一級目錄BUILD.gn文件添加一行編譯引導語句。
import("http://build/ohos.gni")
group("samples") { deps = [ "a25_utils_rwlock:utils_rwlock", # 添加該行 ]}
"a25_utils_rwlock:utils_rwlock",該行語句表示引入 參與編譯。
3.2、創(chuàng)建編譯項目
創(chuàng)建a25_utils_rwlock目錄,并添加如下文件:
a25_utils_rwlock├── utils_rwlock_sample.cpp # .cpp源代碼├──BUILD.gn#GN文件
3.3、創(chuàng)建BUILD.gn
編輯BUILD.gn文件。
import("http://build/ohos.gni")ohos_executable("utils_rwlock") { sources = [ "utils_rwlock_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鍵必須轉化為空格,否則會報錯。如果自己不知道如何規(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)建讀寫鎖和公共資源變量
static OHOS::RWLock m_rwlock(true);static int m_count = 0;
3.4.2、創(chuàng)建線程池并設置
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秒;
上述操作循環(huán)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秒;
上述操作循環(huán)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編譯環(huán)境,運行命令:
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文章
3787瀏覽量
81043 -
系統(tǒng)
+關注
關注
1文章
1017瀏覽量
21344 -
OpenHarmony
+關注
關注
25文章
3722瀏覽量
16317
發(fā)布評論請先 登錄
相關推薦
評論