该程序是基于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秒打印当前运行次数。
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"即可。
2.2、Thread头文件
本案例主要说明线程类提供的相关接口,例如:启动线程、同步通知、异步通知等功能的接口。
C++公共基础类库的Thread头文件在://commonlibrary/c_utils/base/include/thread_ex.h
可在源代码中添加如下:
#include
命令空间如下:
OHOS::Thread
thread_ex.h定义Thread类,该类负责定义Thread类以及相关接口。
构造函数, 构造一个Thread对象,但并不会启动线程。
Thread();
析构函数。
virtual ~Thread();
创建并启动一个子线程,循环执行Run(),当Run()返回false或通知退出时停止。
ThreadStatus Start(const std::string& name, int32_t priority = THREAD_PROI_NORMAL, size_t stack = 0);
参数说明:
返回值说明:
同步通知线程退出,即阻塞式停止子线程。当前线程被阻塞,等待子线程结束。
ThreadStatus NotifyExitSync();
返回值说明:
异步通知线程退出,即子线程退出与否不阻塞当前线程。通知子线程停止,当前线程继续运行。
virtual void NotifyExitAsync();
判断线程是否已经准备就绪,始终返回true。
virtual bool ReadyToWork();
返回值说明:
获取线程退出待定标志位。
bool IsExitPending() const;
返回值说明:
判断线程是否在运行。
bool IsRunning() const;
返回值说明:
获取线程ID。
pthread_t GetThread() const;
需重写Run函数,该部分为用户需要运行的代码。
virtual bool Run() = 0;
在//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 参与编译。
创建a23_utils_thread 目录,并添加如下文件:
a23_utils_thread├── utils_thread_sample.cpp # .cpp源代码├── BUILD.gn # 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
utils_thread_sample.cpp主要功能分别是:
声明子线程类
子线程每1秒打印一段信息
主程序每1秒打印子进程相关信息,第5秒时关闭子进程,再打印5秒的子进程相关信息
具体内容如下:
自定义ThreadSample类,继承OHOS::Thread类。
具体代码如下:
class ThreadSample : public OHOS::Thread {public: ThreadSample() : OHOS::Thread() { } ~ThreadSample() { }
protected: bool Run() override;};
注意:
构造函数ThreadSample()必须执行OHOS::Thread的构造函数,否则无效。
Run()函数为开发者需要重写的函数。该函数为开发者需要启动线程执行的代码。
Run()函数必须添加override关键字,表示要重写该函数。
Run()函数每1秒打印一段信息。
具体代码如下:
bool ThreadSample::Run(){ static int current = 0;
current++; cout << "Run(): current = " << current << endl; sleep(1);
return true;}
注意:
OHOS::Thread类会不断地调用Run()函数,所以该函数只需要写成单循环即可。
主程序每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()是同步关闭线程,在此需要等待线程关闭才会返回。
系统启动后,运行命令:
utils_thread
运行结果:
# 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#
全部0条评论
快来发表一下你的评论吧 !