1、程序簡介
該程序是基于OpenHarmony的C++公共基礎(chǔ)類庫的線程處理:Thread。
該應(yīng)用案例已在OpenHarmony凌蒙派-RK3568開發(fā)板(即OpenHarmony-v3.2.1-release)運行正常,詳細說明及案例源代碼可參考:https://gitee.com/Lockzhiner-Electronics/lockzhiner-rk3568-openharmony/tree/master/samples/a22_utils_thread
本案例完成如下工作:
主線程每1秒打印子進程的相關(guān)信息。主線程在第5秒時,關(guān)閉子線程運行。
創(chuàng)建1個子線程,每隔1秒打印當前運行次數(shù)。
2、基礎(chǔ)知識
C++公共基礎(chǔ)類庫為標準系統(tǒng)提供了一些常用的C++開發(fā)工具類,包括:
文件、路徑、字符串相關(guān)操作的能力增強接口
安全數(shù)據(jù)容器、數(shù)據(jù)序列化等接口
各子系統(tǒng)的錯誤碼相關(guān)定義
2.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.2、Thread頭文件
本案例主要說明線程類提供的相關(guān)接口,例如:啟動線程、同步通知、異步通知等功能的接口。
C++公共基礎(chǔ)類庫的Thread頭文件在://commonlibrary/c_utils/base/include/thread_ex.h
可在源代碼中添加如下:
#include
命令空間如下:
OHOS::Thread
2.3、OHOS::Thread接口說明
thread_ex.h定義Thread類,該類負責(zé)定義Thread類以及相關(guān)接口。
2.3.1、Thread
構(gòu)造函數(shù), 構(gòu)造一個Thread對象,但并不會啟動線程。
Thread();
2.3.2、~Thread
析構(gòu)函數(shù)。
virtual ~Thread();
2.3.3、Start
創(chuàng)建并啟動一個子線程,循環(huán)執(zhí)行Run(),當Run()返回false或通知退出時停止。
ThreadStatus Start(const std::string& name, int32_t priority = THREAD_PROI_NORMAL, size_t stack = 0);
參數(shù)說明:
返回值說明:
2.3.4、NotifyExitSync
同步通知線程退出,即阻塞式停止子線程。當前線程被阻塞,等待子線程結(jié)束。
ThreadStatus NotifyExitSync();
返回值說明:
2.3.5、NotifyExitAsync
異步通知線程退出,即子線程退出與否不阻塞當前線程。通知子線程停止,當前線程繼續(xù)運行。
virtual void NotifyExitAsync();
2.3.6、ReadyToWork
判斷線程是否已經(jīng)準備就緒,始終返回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函數(shù),該部分為用戶需要運行的代碼。
virtual bool Run() = 0;
3、程序解析
3.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 參與編譯。
3.2、創(chuàng)建編譯項目
創(chuàng)建a23_utils_thread 目錄,并添加如下文件:
a23_utils_thread├── utils_thread_sample.cpp # .cpp源代碼├── BUILD.gn # GN文件
3.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}
注意:
(1)BUILD.gn中所有的TAB鍵必須轉(zhuǎn)化為空格,否則會報錯。如果自己不知道如何規(guī)范化,可以:
# 安裝gn工具sudo apt-get install ninja-buildsudo apt install generate-ninja# 規(guī)范化BUILD.gngn format BUILD.gn
3.4、創(chuàng)建源代碼
utils_thread_sample.cpp主要功能分別是:
聲明子線程類
子線程每1秒打印一段信息
主程序每1秒打印子進程相關(guān)信息,第5秒時關(guān)閉子進程,再打印5秒的子進程相關(guān)信息
具體內(nèi)容如下:
3.4.1、編寫子線程類
自定義ThreadSample類,繼承OHOS::Thread類。
具體代碼如下:
class ThreadSample : public OHOS::Thread {public: ThreadSample() : OHOS::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ù)。
3.4.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.4.3、主程序
主程序每1秒打印子進程相關(guān)信息,第5秒時關(guān)閉子進程,再打印5秒的子進程相關(guān)信息。
(1)定義ThreadSample對象并啟用
int main(int argc, char **argv){ ThreadSample thread;
// 啟動線程 thread.Start("thread sample", OHOS::THREAD_PROI_NORMAL, 0); ......}
(2)查看子線程的相關(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);}
(3)第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)閉線程。
(4)同步等待子進程關(guān)閉
thread.NotifyExitSync();
注意:NotifyExitSync()是同步關(guān)閉線程,在此需要等待線程關(guān)閉才會返回。
4、運行程序
系統(tǒng)啟動后,運行命令:
utils_thread
5、運行結(jié)果
運行結(jié)果:
# 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#
-
程序
+關(guān)注
關(guān)注
117文章
3785瀏覽量
81002 -
開發(fā)板
+關(guān)注
關(guān)注
25文章
5032瀏覽量
97371 -
OpenHarmony
+關(guān)注
關(guān)注
25文章
3713瀏覽量
16254
發(fā)布評論請先 登錄
相關(guān)推薦
評論