OpenHarmony C++公共基礎(chǔ)類庫應(yīng)用案例:Thread
想了解更多關(guān)于開源的內(nèi)容,請?jiān)L問:
一、程序簡介
該程序是基于OpenHarmony的C++公共基礎(chǔ)類庫的線程處理:Thread。
該應(yīng)用案例已在OpenHarmony凌蒙派-RK3568開發(fā)板(即OpenHarmony-v3.2.1-release)運(yùn)行正常,詳細(xì)說明及案例源代碼可參考:https://gitee.com/Lockzhiner-Electronics/lockzhiner-rk3568-openharmony/tree/master/samples/a22_utils_thread。
本案例完成如下工作:
- 主線程每1秒打印子進(jìn)程的相關(guān)信息。主線程在第5秒時,關(guān)閉子線程運(yùn)行。
- 創(chuàng)建1個子線程,每隔1秒打印當(dāng)前運(yùn)行次數(shù)。
二、基礎(chǔ)知識
C公共基礎(chǔ)類庫為標(biāo)準(zhǔn)系統(tǒng)提供了一些常用的C開發(fā)工具類,包括:
- 文件、路徑、字符串相關(guān)操作的能力增強(qiáng)接口。
- 讀寫鎖、信號量、定時器、線程增強(qiáng)及線程池等接口。
- 安全數(shù)據(jù)容器、數(shù)據(jù)序列化等接口。
- 各子系統(tǒng)的錯誤碼相關(guān)定義。
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、Thread頭文件
本案例主要說明線程類提供的相關(guān)接口,例如:啟動線程、同步通知、異步通知等功能的接口。
C++公共基礎(chǔ)類庫的Thread頭文件在://commonlibrary/c_utils/base/include/thread_ex.h
可在源代碼中添加如下:
#include <thread_ex.h>
命令空間如下:
OHOS::Thread
3、OHOS::Thread接口說明
thread_ex.h定義Thread類,該類負(fù)責(zé)定義Thread類以及相關(guān)接口。
(1)Thread
構(gòu)造函數(shù), 構(gòu)造一個Thread對象,但并不會啟動線程。
Thread();
(2)、~Thread
析構(gòu)函數(shù)。
virtual ~Thread();
(3)Start
創(chuàng)建并啟動一個子線程,循環(huán)執(zhí)行Run(),當(dāng)Run()返回false或通知退出時停止。
ThreadStatus Start(const std::string& name, int32_t priority = THREAD_PROI_NORMAL, size_t stack = 0);
參數(shù)說明:
返回值說明:
(4)NotifyExitSync
同步通知線程退出,即阻塞式停止子線程。當(dāng)前線程被阻塞,等待子線程結(jié)束。
ThreadStatus NotifyExitSync();
返回值說明:
(5)NotifyExitAsync
異步通知線程退出,即子線程退出與否不阻塞當(dāng)前線程。通知子線程停止,當(dāng)前線程繼續(xù)運(yùn)行。
virtual void NotifyExitAsync();
(6)ReadyToWork
判斷線程是否已經(jīng)準(zhǔn)備就緒,始終返回true。
virtual bool ReadyToWork();
返回值說明:
(7)IsExitPending
獲取線程退出待定標(biāo)志位。
bool IsExitPending() const;
返回值說明:
(8)IsRunning
判斷線程是否在運(yùn)行。
bool IsRunning() const;
返回值說明:
(9)GetThread
獲取線程ID。
pthread_t GetThread() const;
(10)Run
需重寫Run函數(shù),該部分為用戶需要運(yùn)行的代碼。
virtual bool Run() = 0;
三、程序解析
1、創(chuàng)建編譯引導(dǎo)
在//vendor/lockzhiner/rk3568/samples/BUILD.gn文件添加一行編譯引導(dǎo)語句。
import("http://build/ohos.gni")
group("samples") {
deps = [
"a23_utils_thread:utils_thread", # 添加該行
]
}
"a23_utils_thread:utils_thread",該行語句表示引入utils_thread 參與編譯。
2、創(chuàng)建編譯項(xiàng)目
創(chuàng)建a23_utils_thread 目錄,并添加如下文件:
a23_utils_thread
├── utils_thread_sample.cpp # .cpp源代碼
├── BUILD.gn # GN文件
3、創(chuàng)建BUILD.gn
編輯BUILD.gn文件。
import("http://build/ohos.gni")
ohos_executable("utils_thread") {
sources = [ "utils_thread_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
}
注意:
BUILD.gn中所有的TAB鍵必須轉(zhuǎn)化為空格,否則會報錯。如果自己不知道如何規(guī)范化,可以:
# 安裝gn工具
sudo apt-get install ninja-build
sudo apt install generate-ninja
# 規(guī)范化BUILD.gn
gn format BUILD.gn
4、創(chuàng)建源代碼
utils_thread_sample.cpp主要功能分別是:
- 聲明子線程類
- 子線程每1秒打印一段信息
- 主程序每1秒打印子進(jìn)程相關(guān)信息,第5秒時關(guān)閉子進(jìn)程,再打印5秒的子進(jìn)程相關(guān)信息
具體內(nèi)容如下:
(1)編寫子線程類
自定義ThreadSample類,繼承OHOS::Thread類。
具體代碼如下:
class ThreadSample : public OHOS::Thread {
public:
ThreadSample() : OHOS::Thread::Thread()
{
}
~ThreadSample()
{
}
protected:
bool Run() override;
};
注意:
- 構(gòu)造函數(shù)ThreadSample()必須執(zhí)行OHOS::Thread的構(gòu)造函數(shù),否則無效。
- Run()函數(shù)為開發(fā)者需要重寫的函數(shù)。該函數(shù)為開發(fā)者需要啟動線程執(zhí)行的代碼。
- Run()函數(shù)必須添加override關(guān)鍵字,表示要重寫該函數(shù)。
(2)重寫ThreadSample::Run()函數(shù)
Run()函數(shù)每1秒打印一段信息。
具體代碼如下:
bool ThreadSample::Run()
{
static int current = 0;
current++;
cout << "Run(): current = " << current << endl;
sleep(1);
return true;
}
注意:
- OHOS::Thread類會不斷地調(diào)用Run()函數(shù),所以該函數(shù)只需要寫成單循環(huán)即可。
(3)主程序
主程序每1秒打印子進(jìn)程相關(guān)信息,第5秒時關(guān)閉子進(jìn)程,再打印5秒的子進(jìn)程相關(guān)信息。
定義ThreadSample對象并啟用。
int main(int argc, char **argv)
{
ThreadSample thread;
// 啟動線程
thread.Start("thread sample", OHOS::THREAD_PROI_NORMAL, 0);
......
}
查看子線程的相關(guān)數(shù)據(jù)。
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);
}
第5秒后發(fā)起異步關(guān)閉子線程。
for (int i = 0; i < (2 * FORMAX); i++) {
......
if (i == (1 * FORMAX)) {
// 異步停止線程,不用等待,直接返回
cout << "main: NotifyExitAsync" << endl;
thread.NotifyExitAsync();
}
......
sleep(1);
}
注意:NotifyExitAsync()是異步關(guān)閉線程,在此并沒有關(guān)閉線程。
同步等待子進(jìn)程關(guān)閉。
thread.NotifyExitSync();
注意:NotifyExitSync()是同步關(guān)閉線程,在此需要等待線程關(guān)閉才會返回。
四、運(yùn)行程序
系統(tǒng)啟動后,運(yùn)行命令:
utils_thread
五、運(yùn)行結(jié)果
運(yùn)行結(jié)果:
# utils_thread
main: i = 0
ThreadId = 4154539380
ReadyToWork = 1
IsExitPending = 0
IsRunning = 1
Run():
main: i = 1
ThreadId = 4154539380
ReadyToWork = 1
IsExitPending = 0
IsRunning = Run(): 1
main: i = 2
ThreadId = 4154539380
ReadyToWork = 1
IsExitPending = 0
IsRunning = 1
Run():
main: i = 3
ThreadId = Run():
4154539380
ReadyToWork = 1
IsExitPending = 0
IsRunning = 1
Run():
main: i = 4
ThreadId = 4154539380
ReadyToWork = 1
IsExitPending = 0
IsRunning = 1
Run():
main: i = 5
ThreadId = 4154539380
ReadyToWork = 1
IsExitPending = 0
IsRunning = 1
Run():
main: i = 6
ThreadId = 4154539380
ReadyToWork = 1
IsExitPending = 0
IsRunning = 1
Run():
main: i = 7
ThreadId = 4154539380
ReadyToWork = 1
IsExitPending = 0
IsRunning = 1
Run():
main: i = 8
ThreadId = 4154539380
ReadyToWork = 1
IsExitPending = 0
IsRunning = 1
Run():
main: i = 9
ThreadId = 4154539380
ReadyToWork = 1
IsExitPending = 0
IsRunning = 1
Run():
main: i = 10
ThreadId = 4154539380
ReadyToWork = 1
IsExitPending = 0
IsRunning = 1
main: NotifyExitAsync
main: i = 11
ThreadId = 4294967295
ReadyToWork = 1
IsExitPending = 1
IsRunning = 0
main: i = 12
ThreadId = 4294967295
ReadyToWork = 1
IsExitPending = 1
IsRunning = 0
main: i = 13
ThreadId = 4294967295
ReadyToWork = 1
IsExitPending = 1
IsRunning = 0
main: i = 14
ThreadId = 4294967295
ReadyToWork = 1
IsExitPending = 1
IsRunning = 0
#