基于OpenHarmony标准系统的C++公共基础类库案例:ThreadPoll

描述

1、程序简介

该程序是基于OpenHarmony标准系统的C++公共基础类库的线程池处理:ThreadPoll。

本案例完成如下工作:

创建1个线程池,设置该线程池内部有1024个线程空间。

启动5个线程。每个线程每秒打印1段字符串,10秒后停止。

2、基础知识

C++公共基础类库为标准系统提供了一些常用的C++开发工具类,包括:

文件、路径、字符串相关操作的能力增强接口

读写锁、信号量、定时器、线程增强及线程池等接口

安全数据容器、数据序列化等接口

各子系统的错误码相关定义

2.1、添加C++公共基础类库依赖

修改需调用模块的BUILD.gn,在external_deps或deps中添加如下:

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

ohos_shared_library("xxxxx") { ... external_deps = [ ... # 动态库依赖(可选) "c_utils:utils", # 静态库依赖(可选) "c_utils:utilsbase", # Rust动态库依赖(可选) "c_utils:utils_rust", ] ...}

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

2.2、ThreadPoll头文件

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

ThreadPoll维护一个任务队列,一个线程组。开发者只需向任务队列中注册需要进行的任务,线程组执行任务队列中的任务。

C++公共基础类库的Thread头文件在://commonlibrary/c_utils/base/include/thread_pool.h

可在源代码中添加如下:

  •  

#include

命令空间如下:

  •  

OHOS::ThreadPool

2.3、OHOS::Thread接口说明

thread_ex.h定义Thread类,该类负责定义Thread类以及相关接口。

2.3.1、ThreadPool

构造函数, 构造ThreadPool对象,为线程池内线程命名。

  •  

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

参数说明:

参数名称类型参数说明
namestd::string线程名称

2.3.2、~ThreadPool

析构函数。

  •  

~ThreadPool();

2.3.3、AddTask

向任务队列中添加一个Task。若未调用Start()则直接执行Task且不会向任务队列添加该Task。

  •  

void AddTask(const Task& f);

参数说明:

参数名称类型参数说明
fstd::function函数

2.3.4、GetCurTaskNum

获取当前任务数。

  •  

size_t GetCurTaskNum();

返回值说明:

类型返回值说明
size_t返回当前任务数

2.3.5、GetMaxTaskNum

获取最大任务数。

  •  

size_t GetMaxTaskNum() const;

返回值说明:

类型返回值说明
size_t获取最大任务数

2.3.6、GetName

获取线程池命名。

  •  

std::string GetName() const;

返回值说明:

类型返回值说明
std::string线程池命名名称

2.3.7、GetThreadsNum

获取线程池内线程数。

  •  

size_t GetThreadsNum() const;

返回值说明:

类型返回值说明
size_t获取线程池内线程数

2.3.8、SetMaxTaskNum

设置任务队列中最大任务数。

  •  

void SetMaxTaskNum(int maxSize);

参数说明:

参数名称类型参数说明
maxSizeint最大任务数

2.3.9、Start

启动给定数量threadsNum的线程,执行任务队列中的任务。

  •  

uint32_t Start(int threadsNum);

参数说明:

参数名称类型参数说明
threadsNumint需要启动线程的数量

返回值说明:

返回值数值返回值说明
ERR_OK成功
其它错误

2.3.10、Stop

停止线程池,等待所有线程结束。

  •  

void Stop();

3、程序解析

3.1、创建编译引导

在samples/BUILD.gn文件添加一行编译引导语句。

  •  
  •  
  •  
  •  
  •  
  •  
  •  

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

"samples/a24_utils_thread_poll:utils_threadpoll",该行语句表示目录源代码 参与编译。

3.2、创建编译项目

创建samples/a24_utils_thread_poll 目录,并添加如下文件:

  •  
  •  
  •  

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

 

3.3、创建BUILD.gn

编辑BUILD.gn文件。

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

import("//build/ohos.gni")ohos_executable("utils_threadpoll") { sources = [ "utils_thread_poll_sample.cpp" ] include_dirs = [ "//commonlibrary/c_utils/base/include", "//commonlibrary/c_utils/base:utils", "//third_party/googletest:gtest_main", "//third_party/googletest/googletest/include" ] external_deps = [ "c_utils:utils" ] part_name = "product_rk3568" install_enable = true}

注意:

(1)BUILD.gn中所有的TAB键必须转化为空格,否则会报错。如果自己不知道如何规范化,可以:

  •  
  •  
  •  
  •  
  •  

# 安装gn工具sudo apt-get install ninja-buildsudo apt install generate-ninja# 规范化BUILD.gngn format BUILD.gn

3.4、创建源代码

3.4.1、创建线程池

引用头文件,定义OHOS::ThreadPool类对象(即创建线程池)。

  •  

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

3.4.2、获取和设置线程池最大任务数

通过ThreadPool.GetMaxTaskNum()函数获取线程池最大任务数。

通过ThreadPool.SetMaxTaskNum()函数设置线程池最大任务数。

具体代码如下:

  •  

int main(int argc, char **argv){ ...... // 查看默认的线程池最大任务数 cout << "get max task num(default): " << thread_poll.GetMaxTaskNum() << endl; // 设置线程池的最大任务数 cout << "set max task num: " << max_task_num << endl; thread_poll.SetMaxTaskNum(max_task_num); // 再查看线程池最大任务数 cout << "get max task num(set): " << thread_poll.GetMaxTaskNum() << endl; ......}

3.4.3、启动线程池并添加线程

通过ThreadPool.Start()函数启动线程池线程。

通过ThreadPool.AddTask()函数添加线程,并设置执行函数。

具体代码如下:

 

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); // 添加任务到线程池中,并启动运行 thread_poll.AddTask(task); sleep(1); } ......}

 

3.4.4、编写线程执行函数

每秒打印一段信息,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()函数启动线程池线程。

具体代码如下:

 

int main(int argc, char **argv){ // 等待关闭所有的线程,会等待线程池程序全部结束才返回 cout << "stop thread: start" << endl; thread_poll.Stop(); cout << "stop thread: end" << endl; return 0;}

4、运行程序

系统启动后,运行命令:

  •  

utilsthreadpoll

5、运行结果

运行结果:

 

# 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个线程做出打印信息,故上述打印信息各有不同。

 

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分