0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

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

福州市凌睿智捷電子有限公司 ? 2024-08-30 12:42 ? 次閱讀

1、程序簡(jiǎn)介

該程序是基于OpenHarmonyC++公共基礎(chǔ)類庫的讀寫鎖:rwlock。

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

創(chuàng)建3個(gè)讀線程,每個(gè)讀線程循環(huán)5次,每次循環(huán)獲取讀鎖,將公共資源變量打印,睡眠1秒,然后釋放讀鎖,最后再睡眠1秒。

創(chuàng)建3個(gè)寫線程,每個(gè)寫線程循環(huán)5次,每次循環(huán)獲取寫鎖,將公共資源變量打印,睡眠1秒,然后釋放讀鎖,最后再睡眠1秒。

2、基礎(chǔ)知識(shí)

C++公共基礎(chǔ)類庫為標(biāo)準(zhǔn)系統(tǒng)提供了一些常用的C++開發(fā)工具類,包括:

文件、路徑、字符串相關(guān)操作的能力增強(qiáng)接口

讀寫鎖、信號(hào)量、定時(shí)器、線程增強(qiáng)及線程池等接口

安全數(shù)據(jù)容器、數(shù)據(jù)序列化等接口

各子系統(tǒng)的錯(cuò)誤碼相關(guān)定義

2.1、添加C++公共基礎(chǔ)類庫依賴

修改需調(diào)用模塊的BUILD.gn,在external_deps或deps中添加如下:

ohos_shared_library("xxxxx") { ... external_deps = [ ... # 動(dòng)態(tài)庫依賴(可選) "c_utils:utils", # 靜態(tài)庫依賴(可選) "c_utils:utilsbase", # Rust動(dòng)態(tài)庫依賴(可選) "c_utils:utils_rust", ] ...}

一般而言,我們只需要填寫"c_utils:utils"即可。

2.2、rwlock頭文件

C++公共基礎(chǔ)類庫的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

構(gòu)造函數(shù)。

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

參數(shù)說明:

參數(shù)名稱類型參數(shù)說明
writeFirstbool是否優(yōu)先寫模式

2.3.2、~RWLock

析構(gòu)函數(shù)。

~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

構(gòu)造函數(shù)。

UniqueWriteGuard(RWLockable &rwLockable)

參數(shù)說明:

參數(shù)名稱類型參數(shù)說明
rwLockableRWLockabletemplate

2.4.2、~UniqueWriteGuard

析構(gòu)函數(shù)。

~UniqueWriteGuard();

2.5、OHOS::UniqueReadGuard接口說明

2.5.1、UniqueReadGuard

構(gòu)造函數(shù)。

UniqueReadGuard(RWLockable &rwLockable)

參數(shù)說明:

參數(shù)名稱類型參數(shù)說明
rwLockableRWLockabletemplate

2.5.2、~UniqueReadGuard

析構(gòu)函數(shù)。

~UniqueReadGuard();

3、程序解析

3.1、創(chuàng)建編譯引導(dǎo)

在上一級(jí)目錄BUILD.gn文件添加一行編譯引導(dǎo)語句。

import("http://build/ohos.gni")
group("samples") { deps = [ "a25_utils_rwlock:utils_rwlock", # 添加該行 ]}

"a25_utils_rwlock:utils_rwlock",該行語句表示引入 參與編譯。

3.2、創(chuàng)建編譯項(xià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鍵必須轉(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)建讀寫鎖和公共資源變量

static OHOS::RWLock m_rwlock(true);static int m_count = 0;

3.4.2、創(chuàng)建線程池并設(shè)置

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、啟動(dòng)3個(gè)讀線程和3個(gè)寫線程

調(diào)用AddTask()添加子線程。

// 啟動(dòng)讀線程for (int i = 0; i < 3; i++) { str_name = "thread_read_" + to_string(i); auto task = std::bind(funcRead, str_name); threads.AddTask(task);}
// 啟動(dòng)寫線程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、編寫讀線程

讀線程完成如下步驟:

調(diào)用LockRead()獲取讀鎖;

如果獲取到讀鎖,打印公共資源變量,并睡眠1秒;

調(diào)用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、編寫寫線程

寫線程完成如下步驟:

調(diào)用LockWrite()獲取寫鎖;

如果獲取到讀鎖,公共資源變量累加1,并睡眠1秒;

調(diào)用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、編譯步驟

進(jìn)入OpenHarmony編譯環(huán)境,運(yùn)行命令:

hb build -f

5、運(yùn)行結(jié)果

# 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#

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 程序
    +關(guān)注

    關(guān)注

    116

    文章

    3756

    瀏覽量

    80751
  • 系統(tǒng)
    +關(guān)注

    關(guān)注

    1

    文章

    1006

    瀏覽量

    21291
  • OpenHarmony
    +關(guān)注

    關(guān)注

    25

    文章

    3635

    瀏覽量

    16061
收藏 人收藏

    評(píng)論

    相關(guān)推薦

    基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)C++公共基礎(chǔ)案例:ThreadPoll

    1、程序簡(jiǎn)介 該程序是基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)C++公共基礎(chǔ)的線程池處理:Thr
    發(fā)表于 08-12 11:42

    基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)C++公共基礎(chǔ)案例:Semaphore

    1、程序簡(jiǎn)介 該程序是基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)C++公共基礎(chǔ)的線程處理:Semp
    發(fā)表于 08-14 16:38

    基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)C++公共基礎(chǔ)案例:rwlock

    /samples/a25_utils_rwlock 2、基礎(chǔ)知識(shí) C++公共基礎(chǔ)標(biāo)準(zhǔn)系統(tǒng)
    發(fā)表于 08-20 09:37

    基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)C++公共基礎(chǔ)案例:SafeMap

    基礎(chǔ)標(biāo)準(zhǔn)系統(tǒng)提供了一些常用的C++開發(fā)工具,包括: 文件、路徑、字符串相關(guān)操作的能力增強(qiáng)接口 讀寫鎖、信號(hào)量、定時(shí)器、線程增強(qiáng)及線程
    發(fā)表于 08-20 12:00

    基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)C++公共基礎(chǔ)案例:SafeQueue

    /a27_utils_safequeue 2、基礎(chǔ)知識(shí) C++公共基礎(chǔ)標(biāo)準(zhǔn)系統(tǒng)提供了一些常用的C+
    發(fā)表于 08-21 10:56

    基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)C++公共基礎(chǔ)案例:SafeStack

    /a28_utils_safestack 2、基礎(chǔ)知識(shí) C++公共基礎(chǔ)標(biāo)準(zhǔn)系統(tǒng)提供了一些常用的C+
    發(fā)表于 08-21 14:51

    基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)C++公共基礎(chǔ)案例:SafeBlockQueue

    /samples/a29_utils_safeblockqueue 2、基礎(chǔ)知識(shí) C++公共基礎(chǔ)標(biāo)準(zhǔn)系統(tǒng)提供了一些常用的
    發(fā)表于 08-22 10:52

    OpenHarmony C++公共基礎(chǔ)應(yīng)用案例:Thread

    程在第5秒時(shí),關(guān)閉子線程運(yùn)行。 創(chuàng)建1個(gè)子線程,每隔1秒打印當(dāng)前運(yùn)行次數(shù)。 2、基礎(chǔ)知識(shí) C++公共基礎(chǔ)標(biāo)準(zhǔn)系統(tǒng)提供了一些常用的
    發(fā)表于 11-22 11:50

    OpenHarmony C++公共基礎(chǔ)應(yīng)用案例:Thread

    1、程序簡(jiǎn)介該程序是基于OpenHarmonyC++公共基礎(chǔ)的線程處理:Thread。該應(yīng)用案例已在
    的頭像 發(fā)表于 11-23 08:22 ?847次閱讀
    <b class='flag-5'>OpenHarmony</b> <b class='flag-5'>C++</b><b class='flag-5'>公共</b>基礎(chǔ)<b class='flag-5'>類</b><b class='flag-5'>庫</b>應(yīng)用案例:Thread

    OpenHarmony C++公共基礎(chǔ)應(yīng)用案例:HelloWorld

    1、程序簡(jiǎn)介該程序是基于OpenHarmonyC++公共基礎(chǔ)的簡(jiǎn)單案例:HelloWorld。該應(yīng)用案例已在
    的頭像 發(fā)表于 11-23 08:22 ?662次閱讀
    <b class='flag-5'>OpenHarmony</b> <b class='flag-5'>C++</b><b class='flag-5'>公共</b>基礎(chǔ)<b class='flag-5'>類</b><b class='flag-5'>庫</b>應(yīng)用案例:HelloWorld

    OpenHarmony標(biāo)準(zhǔn)系統(tǒng)C++公共基礎(chǔ)案例:HelloWorld

    1、程序簡(jiǎn)介該程序是基于凌蒙派OpenHarmony-v3.2.1標(biāo)準(zhǔn)系統(tǒng)C++公共基礎(chǔ)的簡(jiǎn)
    的頭像 發(fā)表于 08-13 08:23 ?422次閱讀
    <b class='flag-5'>OpenHarmony</b><b class='flag-5'>標(biāo)準(zhǔn)系統(tǒng)</b><b class='flag-5'>C++</b><b class='flag-5'>公共</b>基礎(chǔ)<b class='flag-5'>類</b><b class='flag-5'>庫</b>案例:HelloWorld

    基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)C++公共基礎(chǔ)案例:SafeBlockQueue

    1、程序簡(jiǎn)介該程序是基于OpenHarmonyC++公共基礎(chǔ)的讀寫鎖:SafeBlockQueue。線程安全阻塞隊(duì)列SafeBlock
    的頭像 發(fā)表于 08-30 12:41 ?243次閱讀
    基于<b class='flag-5'>OpenHarmony</b><b class='flag-5'>標(biāo)準(zhǔn)系統(tǒng)</b>的<b class='flag-5'>C++</b><b class='flag-5'>公共</b>基礎(chǔ)<b class='flag-5'>類</b><b class='flag-5'>庫</b>案例:SafeBlockQueue

    基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)C++公共基礎(chǔ)案例:SafeStack

    1、程序簡(jiǎn)介該程序是基于OpenHarmonyC++公共基礎(chǔ)的線程安全隊(duì)列:SafeQueue。線程安全隊(duì)列,是在dequeue的基礎(chǔ)
    的頭像 發(fā)表于 08-30 12:41 ?267次閱讀
    基于<b class='flag-5'>OpenHarmony</b><b class='flag-5'>標(biāo)準(zhǔn)系統(tǒng)</b>的<b class='flag-5'>C++</b><b class='flag-5'>公共</b>基礎(chǔ)<b class='flag-5'>類</b><b class='flag-5'>庫</b>案例:SafeStack

    基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)C++公共基礎(chǔ)案例:SafeQueue

    1、程序簡(jiǎn)介該程序是基于OpenHarmonyC++公共基礎(chǔ)的線程安全隊(duì)列:SafeQueue。線程安全隊(duì)列,是在dequeue的基礎(chǔ)
    的頭像 發(fā)表于 08-30 12:41 ?224次閱讀
    基于<b class='flag-5'>OpenHarmony</b><b class='flag-5'>標(biāo)準(zhǔn)系統(tǒng)</b>的<b class='flag-5'>C++</b><b class='flag-5'>公共</b>基礎(chǔ)<b class='flag-5'>類</b><b class='flag-5'>庫</b>案例:SafeQueue

    基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)C++公共基礎(chǔ)案例:SafeMap

    1、程序簡(jiǎn)介該程序是基于OpenHarmonyC++公共基礎(chǔ)的安全關(guān)聯(lián)容器:SafeMap。Ope
    的頭像 發(fā)表于 08-30 12:42 ?270次閱讀
    基于<b class='flag-5'>OpenHarmony</b><b class='flag-5'>標(biāo)準(zhǔn)系統(tǒng)</b>的<b class='flag-5'>C++</b><b class='flag-5'>公共</b>基礎(chǔ)<b class='flag-5'>類</b><b class='flag-5'>庫</b>案例:SafeMap