1# 润和星闪派物联网开发套件--线程(Thread) 2 3 4 5[润和星闪派物联网开发套件](https://item.taobao.com/item.htm?abbucket=16&id=816685710481&ns=1&priceTId=214783b117346662457694855ed644&skuId=5533042544092&spm=a21n57.sem.item.49.46a639031zWytE&utparam=%7B%22aplus_abtest%22%3A%22b28048df8f009463834be6bdac2a3713%22%7D&xxc=taobaoSearch) 基于海思WS63E解决方案的一套软硬件组合的综合性开发套件。 6 7 8 9## 一、Thread API 10 11| API名称 | 说明 | 12| --------------------- | ------------------------------------------------------ | 13| osThreadNew | 创建一个线程并将其加入活跃线程组中 | 14| osThreadGetName | 返回指定线程的名字 | 15| osThreadGetId | 返回当前运行线程的线程ID | 16| osThreadGetState | 返回当前线程的状态 | 17| osThreadSetPriority | 设置指定线程的优先级 | 18| osThreadGetPriority | 获取当前线程的优先级 | 19| osThreadYield | 将运行控制转交给下一个处于READY状态的线程 | 20| osThreadSuspend | 挂起指定线程的运行 | 21| osThreadResume | 恢复指定线程的运行 | 22| osThreadDetach | 分离指定的线程(当线程终止运行时,线程存储可以被回收) | 23| osThreadJoin | 等待指定线程终止运行 | 24| osThreadExit | 终止当前线程的运行 | 25| osThreadTerminate | 终止指定线程的运行 | 26| osThreadGetStackSize | 获取指定线程的栈空间大小 | 27| osThreadGetStackSpace | 获取指定线程的未使用的栈空间大小 | 28| osThreadGetCount | 获取活跃线程数 | 29| osThreadEnumerate | 获取线程组中的活跃线程数 | 30 31### osThreadNew() 32 33``` 34osThreadId_t osThreadNew(osThreadFunc_t func, void *argument,const osThreadAttr_t *attr ) 35``` 36 37> **注意** :不能在中断服务调用该函数 38 39**参数:** 40 41| 名字 | 描述 | 42| :------- | :------------------------------- | 43| func | 线程函数. | 44| argument | 作为启动参数传递给线程函数的指针 | 45| attr | 线程属性 | 46 47### osThreadTerminate() 48 49``` 50osStatus_t osThreadTerminate (osThreadId_t thread_id) 51``` 52 53| 名字 | 描述 | 54| --------- | ---------------------------------------------------- | 55| thread_id | 指定线程id,该id是由osThreadNew或者osThreadGetId获得 | 56 57## 二、代码分析 58 59创建线程,创建成功则打印线程名字和线程ID 60 61``` 62osThreadId_t newThread(char *name, osThreadFunc_t func, void *arg) { 63 osThreadAttr_t attr = { 64 name, 0, NULL, 0, NULL, 1024*2, osPriorityNormal, 0, 0 65 }; 66 osThreadId_t tid = osThreadNew(func, arg, &attr); 67 if (tid == NULL) { 68 printf("osThreadNew(%s) failed.\r\n", name); 69 } else { 70 printf("osThreadNew(%s) success, thread id: %d.\r\n", name, tid); 71 } 72 return tid; 73} 74``` 75 76该函数首先会打印自己的参数,然后对全局变量count进行循环+1操作,之后会打印count的值 77 78``` 79void threadTest(void *arg) { 80 static int count = 0; 81 printf("%s\r\n",(char *)arg); 82 osThreadId_t tid = osThreadGetId(); 83 printf("threadTest osThreadGetId, thread id:%p\r\n", tid); 84 while (1) { 85 count++; 86 printf("threadTest, count: %d.\r\n", count); 87 osDelay(20); 88 } 89} 90``` 91 92主程序rtosv2_thread_main创建线程并运行,并使用上述API进行相关操作,最后终止所创建的线程。 93 94``` 95void rtosv2_thread_main(void *arg) { 96 (void)arg; 97 osThreadId_t tid=newThread("test_thread", threadTest, "This is a test thread."); 98 99 const char *t_name = osThreadGetName(tid); 100 printf("[Thread Test]osThreadGetName, thread name: %s.\r\n", t_name); 101 102 osThreadState_t state = osThreadGetState(tid); 103 printf("[Thread Test]osThreadGetState, state :%d.\r\n", state); 104 105 osStatus_t status = osThreadSetPriority(tid, osPriorityNormal4); 106 printf("[Thread Test]osThreadSetPriority, status: %d.\r\n", status); 107 108 osPriority_t pri = osThreadGetPriority (tid); 109 printf("[Thread Test]osThreadGetPriority, priority: %d.\r\n", pri); 110 111 status = osThreadSuspend(tid); 112 printf("[Thread Test]osThreadSuspend, status: %d.\r\n", status); 113 114 status = osThreadResume(tid); 115 printf("[Thread Test]osThreadResume, status: %d.\r\n", status); 116 117 uint32_t stacksize = osThreadGetStackSize(tid); 118 printf("[Thread Test]osThreadGetStackSize, stacksize: %d.\r\n", stacksize); 119 120 uint32_t stackspace = osThreadGetStackSpace(tid); 121 printf("[Thread Test]osThreadGetStackSpace, stackspace: %d.\r\n", stackspace); 122 123 uint32_t t_count = osThreadGetCount(); 124 printf("[Thread Test]osThreadGetCount, count: %d.\r\n", t_count); 125 126 osDelay(100); 127 status = osThreadTerminate(tid); 128 printf("[Thread Test]osThreadTerminate, status: %d.\r\n", status); 129} 130``` 131 132## 三、如何编译 133 1341. 将00_thread目录复制到openharmony源码的`applications\sample\wifi-iot\app`目录下, 1352. 修改openharmony源码的`applications\sample\wifi-iot\app\BUILD.gn`文件,将其中的 `features` 改为: 136 137``` 138 features = [ 139 ... 140 "00_thread:thread_demo", 141 ... 142 ] 143``` 1443. 在`device\soc\hisilicon\ws63v100\sdk\build\config\target_config\ws63\config.py`文件中,找到`'ws63-liteos-app'`部分,在其`'ram_component'`中,添加以下代码: 145``` 146"thread_demo" 147``` 148 1494. 在`device\soc\hisilicon\ws63v100\sdk\libs_url\ws63\cmake\ohos.cmake`文件中,找到`"ws63-liteos-app"`部分,在其`set(COMPONENT_LIST`部分,添加以下代码: 150``` 151"thread_demo" 152``` 1535. 在openharmony sdk根目录目录执行:`rm -rf out && hb set -p nearlink_dk_3863 && hb build -f` 154 155## 四、运行结果 156 157设置串口工具波特率为115200,复位开发板,查看打印结果 158 159``` 160[Thread Test] osThreadNew(test_thread) success. 161[Thread Test] osThreadGetName, thread name: test_thread. 162[Thread Test] osThreadGetState, state :1. 163[Thread Test] This is a test thread. <-testThread log 164[Thread Test] threadTest osThreadGetId, thread id:0xe8544 165[Thread Test] threadTest, count: 1. <-testThread log 166[Thread Test] osThreadSetPriority, status: 0. 167[Thread Test] osThreadGetPriority, priority: 28. 168[Thread Test] osThreadSuspend, status: 0. 169[Thread Test] osThreadResume, status: 0. 170[Thread Test] osThreadGetStackSize, stacksize: 2048. 171[Thread Test] osThreadGetStackSpace, stackspace: 1144. 172[Thread Test] osThreadGetCount, count: 12. 173[Thread Test] threadTest, count: 2. <-testThread log 174[Thread Test] threadTest, count: 3. <-testThread log 175[Thread Test] threadTest, count: 4. <-testThread log 176[Thread Test] threadTest, count: 5. <-testThread log 177[Thread Test] threadTest, count: 6. <-testThread log 178[Thread Test] osThreadTerminate, status: 0. 179``` 180 181### 【套件支持】 182 183##### 1. 套件购买 https://item.taobao.com/item.htm?abbucket=16&id=816685710481&ns=1&priceTId=214783b117346662457694855ed644&skuId=5533042544092&spm=a21n57.sem.item.49.46a639031zWytE&utparam=%7B%22aplus_abtest%22%3A%22b28048df8f009463834be6bdac2a3713%22%7D&xxc=taobaoSearch 184 185##### 2. 技术资料 186 187- Gitee码云网站(使用说明书、规格说明书、OpenHarmony开发案例等) **https://gitee.com/hihopeorg_group/near-link** 188- fbb_ws63代码仓(SDK包、技术文档下载)**https://gitee.com/HiSpark/fbb_ws63** 189 190##### 3. 互动交流 191- 海思社区星闪专区-论坛 **https://developer.hisilicon.com/forum/0133146886267870001** 192 193 194