一区二区三区三上|欧美在线视频五区|国产午夜无码在线观看视频|亚洲国产裸体网站|无码成年人影视|亚洲AV亚洲AV|成人开心激情五月|欧美性爱内射视频|超碰人人干人人上|一区二区无码三区亚洲人区久久精品

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

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

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

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

福州市凌睿智捷電子有限公司 ? 2025-02-10 18:09 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

1、程序簡(jiǎn)介

該程序是基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)的C++公共基礎(chǔ)類(lèi)庫(kù)的線(xiàn)程池處理:ThreadPoll。

本案例完成如下工作:

創(chuàng)建1個(gè)線(xiàn)程池,設(shè)置該線(xiàn)程池內(nèi)部有1024個(gè)線(xiàn)程空間。

啟動(dòng)5個(gè)線(xiàn)程。每個(gè)線(xiàn)程每秒打印1段字符串,10秒后停止。

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í)器、線(xiàn)程增強(qiáng)及線(xiàn)程池等接口

安全數(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、ThreadPoll頭文件

ThreadPoll提供線(xiàn)程安全的線(xiàn)程池功能。

ThreadPoll維護(hù)一個(gè)任務(wù)隊(duì)列,一個(gè)線(xiàn)程組。開(kāi)發(fā)者只需向任務(wù)隊(duì)列中注冊(cè)需要進(jìn)行的任務(wù),線(xiàn)程組執(zhí)行任務(wù)隊(duì)列中的任務(wù)。

C++公共基礎(chǔ)類(lèi)庫(kù)的Thread頭文件在://commonlibrary/c_utils/base/include/thread_pool.h

可在源代碼中添加如下:

#include

命令空間如下:

OHOS::ThreadPool

2.3、OHOS::Thread接口說(shuō)明

thread_ex.h定義Thread類(lèi),該類(lèi)負(fù)責(zé)定義Thread類(lèi)以及相關(guān)接口。

2.3.1、ThreadPool

構(gòu)造函數(shù), 構(gòu)造ThreadPool對(duì)象,為線(xiàn)程池內(nèi)線(xiàn)程命名。

explicit ThreadPool(const std::string &name = std::string());

參數(shù)說(shuō)明:

參數(shù)名稱(chēng)類(lèi)型參數(shù)說(shuō)明
namestd::string線(xiàn)程名稱(chēng)

2.3.2、~ThreadPool

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

~ThreadPool();

2.3.3、AddTask

向任務(wù)隊(duì)列中添加一個(gè)Task。若未調(diào)用Start()則直接執(zhí)行Task且不會(huì)向任務(wù)隊(duì)列添加該Task。

void AddTask(const Task& f);

參數(shù)說(shuō)明:

參數(shù)名稱(chēng)類(lèi)型參數(shù)說(shuō)明
fstd::function函數(shù)

2.3.4、GetCurTaskNum

獲取當(dāng)前任務(wù)數(shù)。

size_t GetCurTaskNum();

返回值說(shuō)明:

類(lèi)型返回值說(shuō)明
size_t返回當(dāng)前任務(wù)數(shù)

2.3.5、GetMaxTaskNum

獲取最大任務(wù)數(shù)。

size_t GetMaxTaskNum() const;

返回值說(shuō)明:

類(lèi)型返回值說(shuō)明
size_t獲取最大任務(wù)數(shù)

2.3.6、GetName

獲取線(xiàn)程池命名。

std::string GetName() const;

返回值說(shuō)明:

類(lèi)型返回值說(shuō)明
std::string線(xiàn)程池命名名稱(chēng)

2.3.7、GetThreadsNum

獲取線(xiàn)程池內(nèi)線(xiàn)程數(shù)。

size_t GetThreadsNum() const;

返回值說(shuō)明:

類(lèi)型返回值說(shuō)明
size_t獲取線(xiàn)程池內(nèi)線(xiàn)程數(shù)

2.3.8、SetMaxTaskNum

設(shè)置任務(wù)隊(duì)列中最大任務(wù)數(shù)。

void SetMaxTaskNum(int maxSize);

參數(shù)說(shuō)明:

參數(shù)名稱(chēng)類(lèi)型參數(shù)說(shuō)明
maxSizeint最大任務(wù)數(shù)

2.3.9、Start

啟動(dòng)給定數(shù)量threadsNum的線(xiàn)程,執(zhí)行任務(wù)隊(duì)列中的任務(wù)。

uint32_t Start(int threadsNum);

參數(shù)說(shuō)明:

參數(shù)名稱(chēng)類(lèi)型參數(shù)說(shuō)明
threadsNumint需要啟動(dòng)線(xiàn)程的數(shù)量

返回值說(shuō)明:

返回值數(shù)值返回值說(shuō)明
ERR_OK成功
其它錯(cuò)誤

2.3.10、Stop

停止線(xiàn)程池,等待所有線(xiàn)程結(jié)束。

void Stop();

3、程序解析

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

在samples/BUILD.gn文件添加一行編譯引導(dǎo)語(yǔ)句。

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

"samples/a24_utils_thread_poll:utils_threadpoll",該行語(yǔ)句表示目錄源代碼 參與編譯。

3.2、創(chuàng)建編譯項(xiàng)目

創(chuàng)建samples/a24_utils_thread_poll 目錄,并添加如下文件:

a24_utils_thread_poll├── utils_thread_poll_sample.cpp # .cpp源代碼├──BUILD.gn#GN文件

3.3、創(chuàng)建BUILD.gn

編輯BUILD.gn文件。

import("http://build/ohos.gni")ohos_executable("utils_threadpoll") { sources = [ "utils_thread_poll_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)建線(xiàn)程池

引用頭文件,定義OHOS::ThreadPool類(lèi)對(duì)象(即創(chuàng)建線(xiàn)程池)。

#include // 線(xiàn)程池的頭文件
int main(int argc, char **argv){ OHOS::ThreadPool thread_poll("thread_poll_name"); ......}

3.4.2、獲取和設(shè)置線(xiàn)程池最大任務(wù)數(shù)

通過(guò)ThreadPool.GetMaxTaskNum()函數(shù)獲取線(xiàn)程池最大任務(wù)數(shù)。

通過(guò)ThreadPool.SetMaxTaskNum()函數(shù)設(shè)置線(xiàn)程池最大任務(wù)數(shù)。

具體代碼如下:

int main(int argc, char **argv){ ...... // 查看默認(rèn)的線(xiàn)程池最大任務(wù)數(shù) cout << "get max task num(default): " << thread_poll.GetMaxTaskNum() << endl; // 設(shè)置線(xiàn)程池的最大任務(wù)數(shù) cout << "set max task num: " << max_task_num << endl; thread_poll.SetMaxTaskNum(max_task_num); // 再查看線(xiàn)程池最大任務(wù)數(shù) cout << "get max task num(set): " << thread_poll.GetMaxTaskNum() << endl; ......}

3.4.3、啟動(dòng)線(xiàn)程池并添加線(xiàn)程

通過(guò)ThreadPool.Start()函數(shù)啟動(dòng)線(xiàn)程池線(xiàn)程。

通過(guò)ThreadPool.AddTask()函數(shù)添加線(xiàn)程,并設(shè)置執(zhí)行函數(shù)。

具體代碼如下:

int main(int argc, char **argv){ ...... // 開(kāi)啟啟動(dòng)線(xiàn)程 cout << "start thread: " << start_task_num << endl; thread_poll.Start(start_task_num); for (i = 0; i < start_task_num; i++) { cout << "add task: i = " << i << endl; str_name = "thread_pool_" + to_string(i); auto task = std::bind(func, str_name); // 添加任務(wù)到線(xiàn)程池中,并啟動(dòng)運(yùn)行 thread_poll.AddTask(task); sleep(1); } ......}

3.4.4、編寫(xiě)線(xiàn)程執(zhí)行函數(shù)

每秒打印一段信息,10秒后退出。

void func(const std::string &name){ for (int i = 0; i < 10; i++) { cout << "func: " << name << " and i = " << i << endl; sleep(1); }}

3.4.5、主程序等待線(xiàn)程池全部退出

通過(guò)ThreadPool.Start()函數(shù)啟動(dòng)線(xiàn)程池線(xiàn)程。

具體代碼如下:

int main(int argc, char **argv){ // 等待關(guān)閉所有的線(xiàn)程,會(huì)等待線(xiàn)程池程序全部結(jié)束才返回 cout << "stop thread: start" << endl; thread_poll.Stop(); cout << "stop thread: end" << endl; return 0;}

4、運(yùn)行程序

系統(tǒng)啟動(dòng)后,運(yùn)行命令:

utilsthreadpoll

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

運(yùn)行結(jié)果:

# utils_threadpollget max task num(default): 0set max task num: 1024get max task num(set): 1024start thread: 5add task: i = 0func: thread_pool_0 and i = 0add task: i = 1func: thread_pool_0 and i = 1func: thread_pool_1 and i = 0add task: i = 2func: thread_pool_0 and i = 2func: thread_pool_1 and i = 1func: thread_pool_2 and i = 0add task: i = 3func: thread_pool_0 and i = 3func: thread_pool_1 and i = 2func: thread_pool_3 and i = 0func: thread_pool_2 and i = 1func: thread_pool_0 and i = 4func: thread_pool_3 and i = 2add task: i = 1func: thread_pool_2 and i = 42func: thread_pool_1 and i = 3func: thread_pool_4 and i = 0func: thread_pool_0 and i = 5func: thread_pool_3 and i = 2func: thread_pool_1 and i = 4func: thread_pool_2 and i = 3stop thread: startfunc: thread_pool_4 and i = 1func: thread_pool_0 and i = 6func: thread_pool_1 and i = 5func: thread_pool_3 and i = 3func: thread_pool_4 and i = 2func: thread_pool_2 and i = 4func: thread_pool_0 and i = 7func: thread_pool_1 and i = 6func: thread_pool_3 and i = 4func: thread_pool_2 and i = 5func:thread_pool_4 and i = 3func: thread_pool_0 and i = 8func: thread_pool_1 and i = 7func: thread_pool_3 and i = 5func: thread_pool_2 and i = 6func: thread_pool_4 and i = 4func: thread_pool_0 and i = 9func: thread_pool_1 and i = 8func: thread_pool_3 and i = 6func: thread_pool_2 and i = func: thread_pool_4 and i = 57func: thread_pool_1 and i = 9func: thread_pool_3 and i = 7func: thread_pool_4 and i = 6func: thread_pool_2 and i = 8func: thread_pool_3 and i = 8func: thread_pool_4 and i = 7func: thread_pool_2 and i = 9func: thread_pool_3 and i = 9func: thread_pool_4 and i = 8func: thread_pool_4 and i = 9stop thread: end#

注意:

(1)因有10個(gè)線(xiàn)程做出打印信息,故上述打印信息各有不同。

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

    關(guān)注

    0

    文章

    508

    瀏覽量

    20130
  • OpenHarmony
    +關(guān)注

    關(guān)注

    29

    文章

    3847

    瀏覽量

    18344
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評(píng)論

    相關(guān)推薦
    熱點(diǎn)推薦

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

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

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

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

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

    /samples/a25_utils_rwlock 2、基礎(chǔ)知識(shí) C++公共基礎(chǔ)類(lèi)庫(kù)標(biāo)準(zhǔn)系統(tǒng)提供了一些常用的
    發(fā)表于 08-20 09:37

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

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

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

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

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

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

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

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

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

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

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

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

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

    1、程序簡(jiǎn)介該程序是基于OpenHarmonyC++公共基礎(chǔ)類(lèi)庫(kù)的讀寫(xiě)鎖:SafeBlockQueue。線(xiàn)程安全阻塞隊(duì)列SafeBlock
    的頭像 發(fā)表于 08-30 12:41 ?555次閱讀
    基于<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'>類(lèi)</b><b class='flag-5'>庫(kù)</b>案例:SafeBlockQueue

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

    1、程序簡(jiǎn)介該程序是基于OpenHarmonyC++公共基礎(chǔ)類(lèi)庫(kù)的線(xiàn)程安全隊(duì)列:SafeQueue。線(xiàn)程安全隊(duì)列,是在dequeue的基礎(chǔ)
    的頭像 發(fā)表于 08-30 12:41 ?664次閱讀
    基于<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'>類(lèi)</b><b class='flag-5'>庫(kù)</b>案例:SafeStack

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

    1、程序簡(jiǎn)介該程序是基于OpenHarmonyC++公共基礎(chǔ)類(lèi)庫(kù)的線(xiàn)程安全隊(duì)列:SafeQueue。線(xiàn)程安全隊(duì)列,是在dequeue的基礎(chǔ)
    的頭像 發(fā)表于 08-30 12:41 ?648次閱讀
    基于<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'>類(lèi)</b><b class='flag-5'>庫(kù)</b>案例:SafeQueue

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

    1、程序簡(jiǎn)介該程序是基于OpenHarmonyC++公共基礎(chǔ)類(lèi)庫(kù)的安全關(guān)聯(lián)容器:SafeMap。Ope
    的頭像 發(fā)表于 08-30 12:42 ?734次閱讀
    基于<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'>類(lèi)</b><b class='flag-5'>庫(kù)</b>案例:SafeMap

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

    1、程序簡(jiǎn)介該程序是基于OpenHarmonyC++公共基礎(chǔ)類(lèi)庫(kù)的讀寫(xiě)鎖:rwlock。本案例主要完成如下工作:創(chuàng)建3個(gè)讀線(xiàn)程,每個(gè)讀線(xiàn)程
    的頭像 發(fā)表于 08-30 12:42 ?630次閱讀
    基于<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'>類(lèi)</b><b class='flag-5'>庫(kù)</b>案例:rwlock

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

    1、程序簡(jiǎn)介該程序是基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)C++公共基礎(chǔ)類(lèi)庫(kù)的線(xiàn)程處理:Sempa
    的頭像 發(fā)表于 02-10 18:08 ?310次閱讀
    基于<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'>類(lèi)</b><b class='flag-5'>庫(kù)</b>案例:Semaphore