多线程
项目编写¶
- 在
applications/genkipi/app
下新建thread
文件夹 - 在
thread
下新建main.c
文件 - 在
thread
下新建BUILD.gn
文件
代码部分¶
main.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);
项目Build.gn¶
hello_world
目录下 BUILD.gn
内容为
static_library("thread") {
sources = [
"main.c"
]
include_dirs = [
"//utils/native/lite/include"
]
}
外部Build.gn¶
thread
文件夹上一级目录下BUILD.gn
内容为
import("//build/lite/config/component/lite_component.gni")
lite_component("app") {
features = [
"thread"
]
}