学习目标

  • 能够创建线程
  • 能够启动定时任务
  • 了解如何翻阅源码

    学习内容

    多线程

    线程创建

    ```bash

    include “ohos_init.h”

    include “cmsis_os2.h”

    ….

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”); }

  1. - `attr.name = "thread_1";`
  2. - `attr.stack_size = 1024 * 4;`
  3. - osThreadFunc_t 线程执行的函数
  4. <a name="V0ict"></a>
  5. #### 需求说明
  6. 1. 同时开启两个线程
  7. 2. 每个线程中每隔一段时间进行循环打印计数
  8. 3. 观察打印输出,体会多线程执行的特点
  9. <a name="J7pLk"></a>
  10. #### 开发流程
  11. 来到应用开发的**根目录。**<br />我们编写代码的**根目录**为`device/board/itcast/genkipi/app`
  12. 1. **根目录**下新建`thread`文件夹,此为**项目目录**。
  13. 2. 此**项目目录**下新建`main.c`文件
  14. 3. 此**项目目录**下新建`BUILD.gn`文件
  15. 4. 修改**根目录**下的`BUILD.gn`文件
  16. <a name="ymE7H"></a>
  17. ##### 代码部分
  18. ```c
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include "ohos_init.h"
  23. #include "cmsis_os2.h"
  24. static void task_1(void) {
  25. int count = 0;
  26. while (1) {
  27. printf("task_1----%d\r\n", count++);
  28. usleep(0.5 * 1000 * 1000);
  29. }
  30. }
  31. static void task_2(void) {
  32. int count = 0;
  33. while (1) {
  34. printf("task_2----%d\r\n", count++);
  35. usleep(1 * 1000 * 1000);
  36. }
  37. }
  38. static void start(void) {
  39. osThreadAttr_t attr;
  40. attr.name = "thread_1";
  41. attr.attr_bits = 0U;
  42. attr.cb_mem = NULL;
  43. attr.cb_size = 0U;
  44. attr.stack_mem = NULL;
  45. attr.stack_size = 1024 * 4;
  46. attr.priority = 25;
  47. if (osThreadNew((osThreadFunc_t) task_1, NULL, &attr) == NULL) {
  48. printf("Falied to create task_1!\r\n");
  49. }
  50. attr.name = "thread_2";
  51. if (osThreadNew((osThreadFunc_t) task_2, NULL, &attr) == NULL) {
  52. printf("Falied to create task_2!\n\n");
  53. }
  54. }
  55. APP_FEATURE_INIT(start);
  1. static_library("thread") {
  2. sources = [
  3. "main.c"
  4. ]
  5. }
  1. import("//build/lite/config/component/lite_component.gni")
  2. lite_component("app") {
  3. features = [
  4. "thread"
  5. ]
  6. }

定时器

定时器创建

  1. #include "ohos_init.h"
  2. #include "cmsis_os2.h"
  3. ....
  4. osTimerId_t id;
  5. uint32_t timerDelay;
  6. id = osTimerNew(timer_callback, osTimerPeriodic, NULL, NULL);
  7. if (id != NULL)
  8. {
  9. // Hi3861 1U=10ms,100U=1S
  10. timerDelay = 100U;
  11. status = osTimerStart(id, timerDelay);
  12. if (status != osOK)
  13. {
  14. printf("timer repeat start failed\r\n");
  15. }
  16. }
  • timer_callback: 回调函数
  • osTimerPeriodic: 表示重复执行
  • osTimerOnce: 表示只执行一次

    需求一说明

  1. 开启一个定时器,1秒后执行打印,且只执行一次
  2. 观察打印输出,体会timer定时执行一次的逻辑。

    开发流程

    来到应用开发的根目录。
    我们编写代码的根目录device/board/itcast/genkipi/app

  3. 根目录下新建timer_once文件夹,此为项目目录

  4. 项目目录下新建main.c文件
  5. 项目目录下新建BUILD.gn文件
  6. 修改根目录下的BUILD.gn文件
    代码部分
    ```c

    include

    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;

  1. // once timer
  2. id = osTimerNew(timer_once_callback, osTimerOnce, NULL, NULL);
  3. if (id != NULL)
  4. {
  5. // Hi3861 1U=10ms,100U=1S
  6. timerDelay = 100U;
  7. status = osTimerStart(id, timerDelay);
  8. if (status != osOK)
  9. {
  10. // Timer could not be started
  11. printf("timer once start failed\r\n");
  12. }
  13. }

} APP_FEATURE_INIT(start);

  1. ```c
  2. static_library("timer_once") {
  3. sources = [
  4. "main.c"
  5. ]
  6. }
  1. import("//build/lite/config/component/lite_component.gni")
  2. lite_component("app") {
  3. features = [
  4. "timer_once"
  5. ]
  6. }

需求二说明

  1. 开启一个定时器,间隔1秒执行打印
  2. 观察打印输出,体会timer定时重复执行的逻辑。

    开发流程

    来到应用开发的根目录。
    我们编写代码的根目录device/board/itcast/genkipi/app

  3. 根目录下新建timer_repeat文件夹,此为项目目录

  4. 项目目录下新建main.c文件
  5. 项目目录下新建BUILD.gn文件
  6. 修改根目录下的BUILD.gn文件
    代码部分
    ```c

    include

    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;

  1. // once timer
  2. id = osTimerNew(timer_repeat_callback, osTimerPeriodic, NULL, NULL);
  3. if (id != NULL)
  4. {
  5. // Hi3861 1U=10ms,100U=1S
  6. timerDelay = 100U;
  7. status = osTimerStart(id, timerDelay);
  8. if (status != osOK)
  9. {
  10. // Timer could not be started
  11. printf("timer repeat start failed\r\n");
  12. }
  13. }

} APP_FEATURE_INIT(start);

  1. ```c
  2. static_library("timer_repeat") {
  3. sources = [
  4. "main.c"
  5. ]
  6. }
  1. import("//build/lite/config/component/lite_component.gni")
  2. lite_component("app") {
  3. features = [
  4. "timer_repeat"
  5. ]
  6. }

练习题

  1. 编写多线程示例
  2. 编写定时器示例