学习目标
osThreadAttr_t attr;
attr.name = “thread_1”; attr.attr_bits = 0U; attr.cb_mem = NULL; attr.cb_size = 0U; attr.stack_mem = NULL; attr.stack_size = 1024 * 4; attr.priority = 25;
if (osThreadNew((osThreadFunc_t) task_1, NULL, &attr) == NULL) { printf(“Falied to create task_1!\r\n”); }
- `attr.name = "thread_1";`- `attr.stack_size = 1024 * 4;`- osThreadFunc_t: 线程执行的函数<a name="V0ict"></a>#### 需求说明1. 同时开启两个线程2. 每个线程中每隔一段时间进行循环打印计数3. 观察打印输出,体会多线程执行的特点<a name="J7pLk"></a>#### 开发流程来到应用开发的**根目录。**<br />我们编写代码的**根目录**为`device/board/itcast/genkipi/app`。1. **根目录**下新建`thread`文件夹,此为**项目目录**。2. 此**项目目录**下新建`main.c`文件3. 此**项目目录**下新建`BUILD.gn`文件4. 修改**根目录**下的`BUILD.gn`文件<a name="ymE7H"></a>##### 代码部分```c#include <stdio.h>#include <string.h>#include <unistd.h>#include "ohos_init.h"#include "cmsis_os2.h"static void task_1(void) {int count = 0;while (1) {printf("task_1----%d\r\n", count++);usleep(0.5 * 1000 * 1000);}}static void task_2(void) {int count = 0;while (1) {printf("task_2----%d\r\n", count++);usleep(1 * 1000 * 1000);}}static void start(void) {osThreadAttr_t attr;attr.name = "thread_1";attr.attr_bits = 0U;attr.cb_mem = NULL;attr.cb_size = 0U;attr.stack_mem = NULL;attr.stack_size = 1024 * 4;attr.priority = 25;if (osThreadNew((osThreadFunc_t) task_1, NULL, &attr) == NULL) {printf("Falied to create task_1!\r\n");}attr.name = "thread_2";if (osThreadNew((osThreadFunc_t) task_2, NULL, &attr) == NULL) {printf("Falied to create task_2!\n\n");}}APP_FEATURE_INIT(start);
static_library("thread") {sources = ["main.c"]}
import("//build/lite/config/component/lite_component.gni")lite_component("app") {features = ["thread"]}
定时器
定时器创建
#include "ohos_init.h"#include "cmsis_os2.h"....osTimerId_t id;uint32_t timerDelay;id = osTimerNew(timer_callback, osTimerPeriodic, NULL, NULL);if (id != NULL){// Hi3861 1U=10ms,100U=1StimerDelay = 100U;status = osTimerStart(id, timerDelay);if (status != osOK){printf("timer repeat start failed\r\n");}}
- 开启一个定时器,1秒后执行打印,且只执行一次
-
开发流程
来到应用开发的根目录。
我们编写代码的根目录为device/board/itcast/genkipi/app。 根目录下新建
timer_once文件夹,此为项目目录。- 此项目目录下新建
main.c文件 - 此项目目录下新建
BUILD.gn文件 - 修改根目录下的
BUILD.gn文件代码部分
```cinclude
include
include
include “ohos_init.h”
include “cmsis_os2.h”
static void timer_once_callback(void *arg) { (void)arg; printf(“timer once callback!\r\n”); }
static void start(void) { osTimerId_t id; uint32_t timerDelay; osStatus_t status;
// once timerid = osTimerNew(timer_once_callback, osTimerOnce, NULL, NULL);if (id != NULL){// Hi3861 1U=10ms,100U=1StimerDelay = 100U;status = osTimerStart(id, timerDelay);if (status != osOK){// Timer could not be startedprintf("timer once start failed\r\n");}}
} APP_FEATURE_INIT(start);
```cstatic_library("timer_once") {sources = ["main.c"]}
import("//build/lite/config/component/lite_component.gni")lite_component("app") {features = ["timer_once"]}
需求二说明
- 开启一个定时器,间隔1秒执行打印
-
开发流程
来到应用开发的根目录。
我们编写代码的根目录为device/board/itcast/genkipi/app。 根目录下新建
timer_repeat文件夹,此为项目目录。- 此项目目录下新建
main.c文件 - 此项目目录下新建
BUILD.gn文件 - 修改根目录下的
BUILD.gn文件代码部分
```cinclude
include
include
include “ohos_init.h”
include “cmsis_os2.h”
static void timer_repeat_callback(void *arg) { (void)arg; printf(“timer repeat callback!\r\n”); }
static void start(void) { osTimerId_t id; uint32_t timerDelay; osStatus_t status;
// once timerid = osTimerNew(timer_repeat_callback, osTimerPeriodic, NULL, NULL);if (id != NULL){// Hi3861 1U=10ms,100U=1StimerDelay = 100U;status = osTimerStart(id, timerDelay);if (status != osOK){// Timer could not be startedprintf("timer repeat start failed\r\n");}}
} APP_FEATURE_INIT(start);
```cstatic_library("timer_repeat") {sources = ["main.c"]}
import("//build/lite/config/component/lite_component.gni")lite_component("app") {features = ["timer_repeat"]}
练习题
- 编写多线程示例
- 编写定时器示例
