OpenHarmony C++公共基础类库应用案例:Thread

1、程序简介该程序是基于openharmony的c++公共基础类库的线程处理:thread。
该应用案例已在openharmony凌蒙派-rk3568开发板(即openharmony-v3.2.1-release)运行正常,详细说明及案例源代码可参考:https://gitee.com/lockzhiner-electronics/lockzhiner-rk3568-openharmony/tree/master/samples/a22_utils_thread
本案例完成如下工作:
主线程每1秒打印子进程的相关信息。主线程在第5秒时,关闭子线程运行。
创建1个子线程,每隔1秒打印当前运行次数。
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、thread头文件
本案例主要说明线程类提供的相关接口,例如:启动线程、同步通知、异步通知等功能的接口。
c++公共基础类库的thread头文件在://commonlibrary/c_utils/base/include/thread_ex.h
可在源代码中添加如下:
#include
命令空间如下:
 ohos::thread
2.3、ohos::thread接口说明thread_ex.h定义thread类,该类负责定义thread类以及相关接口。
2.3.1、thread构造函数, 构造一个thread对象,但并不会启动线程。
 thread();
2.3.2、~thread析构函数。
 virtual ~thread();
2.3.3、start创建并启动一个子线程,循环执行run(),当run()返回false或通知退出时停止。
 threadstatus start(const std::string& name, int32_t priority = thread_proi_normal, size_t stack = 0);
参数说明:
返回值说明:
2.3.4、notifyexitsync同步通知线程退出,即阻塞式停止子线程。当前线程被阻塞,等待子线程结束。
 threadstatus notifyexitsync();
返回值说明:
2.3.5、notifyexitasync异步通知线程退出,即子线程退出与否不阻塞当前线程。通知子线程停止,当前线程继续运行。
 virtual void notifyexitasync();
2.3.6、readytowork判断线程是否已经准备就绪,始终返回true。
 virtual bool readytowork();
返回值说明:
2.3.7、isexitpending获取线程退出待定标志位。
 bool isexitpending() const;
返回值说明:
2.3.8、isrunning判断线程是否在运行。
 bool isrunning() const;
返回值说明:
2.3.9、getthread获取线程id。
 pthread_t getthread() const;
2.3.10、run需重写run函数,该部分为用户需要运行的代码。
 virtual bool run() = 0;
3、程序解析3.1、创建编译引导在//vendor/lockzhiner/rk3568/samples/build.gn文件添加一行编译引导语句。
        import(//build/ohos.gni)
group(samples) { deps = [ a23_utils_thread:utils_thread, # 添加该行 ]}
a23_utils_thread:utils_thread,该行语句表示引入utils_thread 参与编译。
3.2、创建编译项目创建a23_utils_thread 目录,并添加如下文件:
   a23_utils_thread├── utils_thread_sample.cpp # .cpp源代码├── build.gn # gn文件
3.3、创建build.gn编辑build.gn文件。
               import(//build/ohos.gni)ohos_executable(utils_thread) { sources = [ utils_thread_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、创建源代码utils_thread_sample.cpp主要功能分别是:
声明子线程类
子线程每1秒打印一段信息
主程序每1秒打印子进程相关信息,第5秒时关闭子进程,再打印5秒的子进程相关信息
具体内容如下:
3.4.1、编写子线程类自定义threadsample类,继承ohos::thread类。
具体代码如下:
             class threadsample : public ohos::thread {public: threadsample() : ohos::thread() { } ~threadsample() { }
protected: bool run() override;};
注意:
构造函数threadsample()必须执行ohos::thread的构造函数,否则无效。
run()函数为开发者需要重写的函数。该函数为开发者需要启动线程执行的代码。
run()函数必须添加override关键字,表示要重写该函数。
3.4.2、重写threadsample::run()函数run()函数每1秒打印一段信息。
具体代码如下:
            bool threadsample::run(){ static int current = 0;
current++; cout << run(): current = << current << endl; sleep(1);
return true;}
注意:
ohos::thread类会不断地调用run()函数,所以该函数只需要写成单循环即可。
3.4.3、主程序主程序每1秒打印子进程相关信息,第5秒时关闭子进程,再打印5秒的子进程相关信息。
(1)定义threadsample对象并启用
         int main(int argc, char **argv){ threadsample thread;
// 启动线程 thread.start(thread sample, ohos::thread_proi_normal, 0); ......}
(2)查看子线程的相关数据
         for (int i = 0; i < (2 * formax); i++) { cout << main: i = << i << endl; cout << threadid = << thread.getthread() << endl; cout << readytowork = << thread.readytowork() << endl; cout << isexitpending = << thread.isexitpending() << endl; cout << isrunning = << thread.isrunning() << endl; ...... sleep(1);}
(3)第5秒后发起异步关闭子线程
          for (int i = 0; i < (2 * formax); i++) { ...... if (i == (1 * formax)) { // 异步停止线程,不用等待,直接返回 cout << main: notifyexitasync << endl; thread.notifyexitasync(); } ...... sleep(1);}
注意:notifyexitasync()是异步关闭线程,在此并没有关闭线程。
(4)同步等待子进程关闭
 thread.notifyexitsync();
注意:notifyexitsync()是同步关闭线程,在此需要等待线程关闭才会返回。
4、运行程序系统启动后,运行命令:
 utils_thread
5、运行结果运行结果:
                                                                                          # utils_threadmain: i = 0 threadid = 4154539380 readytowork = 1 isexitpending = 0 isrunning = 1run():main: i = 1 threadid = 4154539380 readytowork = 1 isexitpending = 0 isrunning = run(): 1
main: i = 2 threadid = 4154539380 readytowork = 1 isexitpending = 0 isrunning = 1run():main: i = 3 threadid = run():4154539380 readytowork = 1 isexitpending = 0 isrunning = 1run(): main: i = 4 threadid = 4154539380 readytowork = 1 isexitpending = 0 isrunning = 1run(): main: i = 5 threadid = 4154539380 readytowork = 1 isexitpending = 0 isrunning = 1run(): main: i = 6 threadid = 4154539380 readytowork = 1 isexitpending = 0 isrunning = 1run(): main: i = 7 threadid = 4154539380 readytowork = 1 isexitpending = 0 isrunning = 1run(): main: i = 8 threadid = 4154539380 readytowork = 1 isexitpending = 0 isrunning = 1run(): main: i = 9 threadid = 4154539380 readytowork = 1 isexitpending = 0 isrunning = 1run(): main: i = 10 threadid = 4154539380 readytowork = 1 isexitpending = 0 isrunning = 1main: notifyexitasyncmain: i = 11 threadid = 4294967295 readytowork = 1 isexitpending = 1 isrunning = 0main: i = 12 threadid = 4294967295 readytowork = 1 isexitpending = 1 isrunning = 0main: i = 13 threadid = 4294967295 readytowork = 1 isexitpending = 1 isrunning = 0main: i = 14 threadid = 4294967295 readytowork = 1 isexitpending = 1 isrunning = 0#

从入门到高手,电子工程师必备的15款工具!
《2019物联网行业100强》榜单新鲜出炉,SIMBOSS荣登100强
室外光缆冷接技术存在明显缺陷
STM32U5系列TIMER+DMA+DAC应用演示
阿甘精神加持 华米科技8月27日召开年度新品发布会
OpenHarmony C++公共基础类库应用案例:Thread
配置文档(ini文档)的应用
简单介绍一下什么是微波通讯?
比特币早期历史简介
美政府“激励”,中国推进本国芯片产业的发展
模式带宽在光纤测试中的用途
通信基站是否会对信号屏蔽器的效果造成影响
外资车企的未来布局:一场针对中国电池企业的争夺战
磁电式传感器结构示意图(转速转矩传感器结构原理图)
PCB设计领域的DRC和MRC之间有什么区别?
氮化镓用途和性质
中电科南京外延材料产业基地投产,一期投资19.3亿元
三星8个系列电视机通过TÜV莱茵&quot;产品碳减排&quot;核查
裸视式3D显示技术详解
芯片上车之如何跑好质量管控“马拉松”