OpenHarmony开发05 —— 操作系统实验之线程
[实验指导文档](OpenHarmony/vendor_hisilicon - Gitee.com)
-
首先将代码
thread_demo/00_thread
拷贝到程序目录下 -
参照指导文档,我们修改
app/BUILD.gn
,并且修改:import("//build/lite/config/component/lite_component.gni") lite_component("app") { features = [ "iothardware:thread_demo", ] }
-
在openharmony源码顶层目录执行
python3 build.py wifiiot
发现报错
[OHOS ERROR] /home/ros/Documents/DevEco/Projects/OpenHarmony-v3.1-Release/build/lite/components/ohos_builds not found
-
在网上一番搜索,先执行
python3 -m pip install --user ohos-build
-
但是之后也没有解决,于是换一种思路,直接更改
my_first_app
// helloworld.c #include <stdio.h> #include <unistd.h> #include "ohos_init.h" #include "cmsis_os2.h" #define STACK_SIZE 1024 #define OS_DELAY 100 #define OS_DELAYONE 20 osThreadId_t newThread(char *name, osThreadFunc_t func, int*arg) { osThreadAttr_t attr = { name, 0, NULL, 0, NULL, 1024*2, osPriorityNormal, 0, 0 }; osThreadId_t tid = osThreadNew(func, arg, &attr); if (tid == NULL) { printf("[Thread Test] osThreadNew(%s) failed.\r\n", name); } else { printf("[Thread Test] osThreadNew(%s) success, thread id: %d.\r\n", name, tid); } return tid; } void threadTest(int *arg) { static int count = 0; printf("%s\r\n", (char *)arg); osThreadId_t tid = osThreadGetId(); printf("[Thread Test] threadTest osThreadGetId, thread id:%p\r\n", tid); while (1) { count++; printf("[Thread Test] threadTest, count: %d.\r\n", count); osDelay(OS_DELAYONE); } } void rtosv2_thread_main(int *arg) { (void)arg; osThreadId_t tid = newThread("test_thread", threadTest, "This is a test thread."); const char *t_name = osThreadGetName(tid); printf("[Thread Test] osThreadGetName, thread name: %s.\r\n", t_name); osThreadState_t state = osThreadGetState(tid); printf("[Thread Test] osThreadGetState, state :%d.\r\n", state); osStatus_t status = osThreadSetPriority(tid, osPriorityNormal4); printf("[Thread Test] osThreadSetPriority, status: %d.\r\n", status); osPriority_t pri = osThreadGetPriority(tid); printf("[Thread Test] osThreadGetPriority, priority: %d.\r\n", pri); status = osThreadSuspend(tid); printf("[Thread Test] osThreadSuspend, status: %d.\r\n", status); status = osThreadResume(tid); printf("[Thread Test] osThreadResume, status: %d.\r\n", status); uint32_t stacksize = osThreadGetStackSize(tid); printf("[Thread Test] osThreadGetStackSize, stacksize: %u.\r\n", stacksize); uint32_t stackspace = osThreadGetStackSpace(tid); printf("[Thread Test] osThreadGetStackSpace, stackspace: %u.\r\n", stackspace); uint32_t t_count = osThreadGetCount(); printf("[Thread Test] osThreadGetCount, count: %u.\r\n", t_count); osDelay(OS_DELAY); status = osThreadTerminate(tid); printf("[Thread Test] osThreadTerminate, status: %d.\r\n", status); } void HelloWorld(void) { osThreadAttr_t attr; attr.name = "rtosv2_thread_main"; attr.attr_bits = 0U; attr.cb_mem = NULL; attr.cb_size = 0U; attr.stack_mem = NULL; attr.stack_size = STACK_SIZE; attr.priority = osPriorityNormal; if (osThreadNew((osThreadFunc_t)rtosv2_thread_main, NULL, &attr) == NULL) { printf("[ThreadTestTask] Failed to create rtosv2_thread_main!\n"); } } //SYS_RUN(HelloWorld); APP_FEATURE_INIT(HelloWorld);
//build.gn
static_library("myapp") {
sources = [
"hello_world.c",
]
include_dirs = [
"//utils/native/lite/include",
"//commonlibrary/utils_lite/include",
"//kernel/liteos_m/components/cmsis/2.0",
]
}
-
运行结果: