chinese直男口爆体育生外卖, 99久久er热在这里只有精品99, 又色又爽又黄18禁美女裸身无遮挡, gogogo高清免费观看日本电视,私密按摩师高清版在线,人妻视频毛茸茸,91论坛 兴趣闲谈,欧美 亚洲 精品 8区,国产精品久久久久精品免费

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

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

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

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

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

掃碼添加小助手

加入工程師交流群

1、程序簡介

該程序是基于OpenHarmony標準系統(tǒng)的C++公共基礎(chǔ)類庫的線程池處理:ThreadPoll。

本案例完成如下工作:

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

啟動5個線程。每個線程每秒打印1段字符串,10秒后停止。

2、基礎(chǔ)知識

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

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

讀寫鎖、信號量、定時器、線程增強及線程池等接口

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

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

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

修改需調(diào)用模塊的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、ThreadPoll頭文件

ThreadPoll提供線程安全的線程池功能。

ThreadPoll維護一個任務(wù)隊列,一個線程組。開發(fā)者只需向任務(wù)隊列中注冊需要進行的任務(wù),線程組執(zhí)行任務(wù)隊列中的任務(wù)。

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

可在源代碼中添加如下:

#include

命令空間如下:

OHOS::ThreadPool

2.3、OHOS::Thread接口說明

thread_ex.h定義Thread類,該類負責定義Thread類以及相關(guān)接口。

2.3.1、ThreadPool

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

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

參數(shù)說明:

參數(shù)名稱類型參數(shù)說明
namestd::string線程名稱

2.3.2、~ThreadPool

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

~ThreadPool();

2.3.3、AddTask

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

void AddTask(const Task& f);

參數(shù)說明:

參數(shù)名稱類型參數(shù)說明
fstd::function函數(shù)

2.3.4、GetCurTaskNum

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

size_t GetCurTaskNum();

返回值說明:

類型返回值說明
size_t返回當前任務(wù)數(shù)

2.3.5、GetMaxTaskNum

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

size_t GetMaxTaskNum() const;

返回值說明:

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

2.3.6、GetName

獲取線程池命名。

std::string GetName() const;

返回值說明:

類型返回值說明
std::string線程池命名名稱

2.3.7、GetThreadsNum

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

size_t GetThreadsNum() const;

返回值說明:

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

2.3.8、SetMaxTaskNum

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

void SetMaxTaskNum(int maxSize);

參數(shù)說明:

參數(shù)名稱類型參數(shù)說明
maxSizeint最大任務(wù)數(shù)

2.3.9、Start

啟動給定數(shù)量threadsNum的線程,執(zhí)行任務(wù)隊列中的任務(wù)。

uint32_t Start(int threadsNum);

參數(shù)說明:

參數(shù)名稱類型參數(shù)說明
threadsNumint需要啟動線程的數(shù)量

返回值說明:

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

2.3.10、Stop

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

void Stop();

3、程序解析

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

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

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

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

3.2、創(chuà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)化為空格,否則會報錯。如果自己不知道如何規(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)建線程池

引用頭文件,定義OHOS::ThreadPool類對象(即創(chuàng)建線程池)。

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

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

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

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

具體代碼如下:

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

3.4.3、啟動線程池并添加線程

通過ThreadPool.Start()函數(shù)啟動線程池線程。

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

具體代碼如下:

int main(int argc, char **argv){ ...... // 開啟啟動線程 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ù)到線程池中,并啟動運行 thread_poll.AddTask(task); sleep(1); } ......}

3.4.4、編寫線程執(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、主程序等待線程池全部退出

通過ThreadPool.Start()函數(shù)啟動線程池線程。

具體代碼如下:

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

4、運行程序

系統(tǒng)啟動后,運行命令:

utilsthreadpoll

5、運行結(jié)果

運行結(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個線程做出打印信息,故上述打印信息各有不同。

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

    關(guān)注

    0

    文章

    508

    瀏覽量

    20649
  • OpenHarmony
    +關(guān)注

    關(guān)注

    31

    文章

    3891

    瀏覽量

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

掃碼添加小助手

加入工程師交流群

    評論

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

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

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

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

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

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

    /samples/a25_utils_rwlock 2、基礎(chǔ)知識 C++公共基礎(chǔ)標準系統(tǒng)提供了一些常用的
    發(fā)表于 08-20 09:37

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

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

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

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

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

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

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

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

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

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

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

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

    1、程序簡介該程序是基于OpenHarmonyC++公共基礎(chǔ)的安全關(guān)聯(lián)容器:SafeMap。Ope
    的頭像 發(fā)表于 08-30 12:42 ?956次閱讀
    基于<b class='flag-5'>OpenHarmony</b><b class='flag-5'>標準系統(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

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

    1、程序簡介該程序是基于OpenHarmonyC++公共基礎(chǔ)的讀寫鎖:rwlock。本案例主要完成如下工作:創(chuàng)建3個讀線程,每個讀線程
    的頭像 發(fā)表于 08-30 12:42 ?768次閱讀
    基于<b class='flag-5'>OpenHarmony</b><b class='flag-5'>標準系統(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>案例:rwlock

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

    1、程序簡介該程序是基于OpenHarmony標準系統(tǒng)C++公共基礎(chǔ)的線程處理:Sempa
    的頭像 發(fā)表于 02-10 18:08 ?515次閱讀
    基于<b class='flag-5'>OpenHarmony</b><b class='flag-5'>標準系統(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>案例:Semaphore