在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

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

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

基于OpenHarmony標準系統(tǒng)的C++公共基礎類庫案例:rwlock

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

1、程序簡介

該程序是基于OpenHarmonyC++公共基礎類庫的讀寫鎖: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);

參數說明:

參數名稱類型參數說明
writeFirstbool是否優(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)

參數說明:

參數名稱類型參數說明
rwLockableRWLockabletemplate

2.4.2、~UniqueWriteGuard

析構函數。

~UniqueWriteGuard();

2.5、OHOS::UniqueReadGuard接口說明

2.5.1、UniqueReadGuard

構造函數。

UniqueReadGuard(RWLockable &rwLockable)

參數說明:

參數名稱類型參數說明
rwLockableRWLockabletemplate

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#

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

    關注

    117

    文章

    3787

    瀏覽量

    81043
  • 系統(tǒng)
    +關注

    關注

    1

    文章

    1017

    瀏覽量

    21344
  • OpenHarmony
    +關注

    關注

    25

    文章

    3722

    瀏覽量

    16317
收藏 人收藏

    評論

    相關推薦

    基于OpenHarmony標準系統(tǒng)C++公共基礎案例:ThreadPoll

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

    基于OpenHarmony標準系統(tǒng)C++公共基礎案例:Semaphore

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

    基于OpenHarmony標準系統(tǒng)C++公共基礎案例:rwlock

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

    基于OpenHarmony標準系統(tǒng)C++公共基礎案例:SafeMap

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

    基于OpenHarmony標準系統(tǒng)C++公共基礎案例:SafeQueue

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

    基于OpenHarmony標準系統(tǒng)C++公共基礎案例:SafeStack

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

    基于OpenHarmony標準系統(tǒng)C++公共基礎案例:SafeBlockQueue

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

    OpenHarmony C++公共基礎應用案例:Thread

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

    OpenHarmony C++公共基礎應用案例:Thread

    1、程序簡介該程序是基于OpenHarmonyC++公共基礎的線程處理:Thread。該應用案例已在
    的頭像 發(fā)表于 11-23 08:22 ?924次閱讀
    <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。該應用案例已在
    的頭像 發(fā)表于 11-23 08:22 ?703次閱讀
    <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標準系統(tǒng)C++公共基礎案例:HelloWorld

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

    基于OpenHarmony標準系統(tǒng)C++公共基礎案例:SafeBlockQueue

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

    基于OpenHarmony標準系統(tǒng)C++公共基礎案例:SafeStack

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

    基于OpenHarmony標準系統(tǒng)C++公共基礎案例:SafeQueue

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

    基于OpenHarmony標準系統(tǒng)C++公共基礎案例:SafeMap

    1、程序簡介該程序是基于OpenHarmonyC++公共基礎的安全關聯(lián)容器:SafeMap。Ope
    的頭像 發(fā)表于 08-30 12:42 ?359次閱讀
    基于<b class='flag-5'>OpenHarmony</b><b class='flag-5'>標準系統(tǒng)</b>的<b class='flag-5'>C++</b><b class='flag-5'>公共</b>基礎<b class='flag-5'>類</b><b class='flag-5'>庫</b>案例:SafeMap
    主站蜘蛛池模板: 久久久久久免费观看| 国产福利毛片| 六月丁香激情综合成人| 亚洲天堂导航| 久草tv| 色偷偷尼玛图亚洲综合| 亚洲男人的性天堂| 日韩毛片免费视频一级特黄| 夜夜夜久久久| 中文字幕一区在线观看| 色多多视频在线观看免费大全| 91色视频网站| 91拍拍在线观看| 日韩一级影院| 天天射天天色天天干| 欧美亚洲在线| 久久综合九色综合欧洲| 午夜宅男视频| 国产精品九九久久一区hh| 4438x五月天| 色成年激情久久综合| 午夜高清| 美女无遮挡拍拍拍免费视频| 看全色黄大色大片免费久久怂| 久久伊人影视| 迅雷www天堂在线资源| a毛片免费观看完整| 久久影院午夜伦手机不四虎卡| 日韩乱轮| 美女黄18以下禁止观看| 亚洲a在线播放| 新版天堂8在线天堂| 国产精品女丝袜白丝袜| 国产成年美女毛片80s| 91男人| 久久青草国产精品一区| www色中色| 人人爽人人爱| 国产chinesetube| 三级网址在线| 国产小视频在线播放|