该程序是基于OpenHarmony标准系统的C++公共基础类库的线程池处理:ThreadPoll。
本案例完成如下工作:
创建1个线程池,设置该线程池内部有1024个线程空间。
启动5个线程。每个线程每秒打印1段字符串,10秒后停止。
C++公共基础类库为标准系统提供了一些常用的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"即可。
ThreadPoll提供线程安全的线程池功能。
ThreadPoll维护一个任务队列,一个线程组。开发者只需向任务队列中注册需要进行的任务,线程组执行任务队列中的任务。
C++公共基础类库的Thread头文件在://commonlibrary/c_utils/base/include/thread_pool.h
可在源代码中添加如下:
#include
命令空间如下:
OHOS::ThreadPool
thread_ex.h定义Thread类,该类负责定义Thread类以及相关接口。
构造函数, 构造ThreadPool对象,为线程池内线程命名。
explicit ThreadPool(const std::string &name = std::string());
参数说明:
参数名称 | 类型 | 参数说明 |
---|---|---|
name | std::string | 线程名称 |
析构函数。
~ThreadPool();
向任务队列中添加一个Task。若未调用Start()则直接执行Task且不会向任务队列添加该Task。
void AddTask(const Task& f);
参数说明:
参数名称 | 类型 | 参数说明 |
---|---|---|
f | std::function | 函数 |
获取当前任务数。
size_t GetCurTaskNum();
返回值说明:
类型 | 返回值说明 |
---|---|
size_t | 返回当前任务数 |
获取最大任务数。
size_t GetMaxTaskNum() const;
返回值说明:
类型 | 返回值说明 |
---|---|
size_t | 获取最大任务数 |
获取线程池命名。
std::string GetName() const;
返回值说明:
类型 | 返回值说明 |
---|---|
std::string | 线程池命名名称 |
获取线程池内线程数。
size_t GetThreadsNum() const;
返回值说明:
类型 | 返回值说明 |
---|---|
size_t | 获取线程池内线程数 |
设置任务队列中最大任务数。
void SetMaxTaskNum(int maxSize);
参数说明:
参数名称 | 类型 | 参数说明 |
---|---|---|
maxSize | int | 最大任务数 |
启动给定数量threadsNum的线程,执行任务队列中的任务。
uint32_t Start(int threadsNum);
参数说明:
参数名称 | 类型 | 参数说明 |
---|---|---|
threadsNum | int | 需要启动线程的数量 |
返回值说明:
返回值数值 | 返回值说明 |
---|---|
ERR_OK | 成功 |
其它 | 错误 |
停止线程池,等待所有线程结束。
void Stop();
在samples/BUILD.gn文件添加一行编译引导语句。
import("//build/ohos.gni")
group("samples") { deps = [ "a24_utils_thread_poll:utils_threadpoll", # 添加该行 ]}
"samples/a24_utils_thread_poll:utils_threadpoll",该行语句表示目录源代码 参与编译。
创建samples/a24_utils_thread_poll 目录,并添加如下文件:
a24_utils_thread_poll├── utils_thread_poll_sample.cpp # .cpp源代码├── BUILD.gn # 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
引用头文件,定义OHOS::ThreadPool类对象(即创建线程池)。
#include // 线程池的头文件
int main(int argc, char **argv){ OHOS::ThreadPool thread_poll("thread_poll_name"); ......}
通过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; ......}
通过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); } ......}
每秒打印一段信息,10秒后退出。
void func(const std::string &name){ for (int i = 0; i < 10; i++) { cout << "func: " << name << " and i = " << i << endl; sleep(1); }}
通过ThreadPool.Start()函数启动线程池线程。
具体代码如下:
int main(int argc, char **argv){ // 等待关闭所有的线程,会等待线程池程序全部结束才返回 cout << "stop thread: start" << endl; thread_poll.Stop(); cout << "stop thread: end" << endl; return 0;}
系统启动后,运行命令:
utilsthreadpoll
运行结果:
# 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个线程做出打印信息,故上述打印信息各有不同。
全部0条评论
快来发表一下你的评论吧 !