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ō)明 |
---|---|---|
name | std::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ō)明 |
---|---|---|
f | std::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ō)明 |
---|---|---|
maxSize | int | 最大任務(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ō)明 |
---|---|---|
threadsNum | int | 需要啟動(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)程做出打印信息,故上述打印信息各有不同。
-
線(xiàn)程
+關(guān)注
關(guān)注
0文章
508瀏覽量
20130 -
OpenHarmony
+關(guān)注
關(guān)注
29文章
3847瀏覽量
18344
發(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ù)案例: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)用案例: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ù)案例:SafeMap

基于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

評(píng)論