OpenHarmony开发05 —— 操作系统实验之线程

OpenHarmony开发05 —— 操作系统实验之线程

[实验指导文档](OpenHarmony/vendor_hisilicon - Gitee.com)

  1. 首先将代码thread_demo/00_thread拷贝到程序目录下

  2. 参照指导文档,我们修改app/BUILD.gn,并且修改:

    import("//build/lite/config/component/lite_component.gni")
    
    lite_component("app") {
        features = [
            "iothardware:thread_demo",
        ]
    }
    
  3. 在openharmony源码顶层目录执行python3 build.py wifiiot

    image

    发现报错[OHOS ERROR] /home/ros/Documents/DevEco/Projects/OpenHarmony-v3.1-Release/build/lite/components/ohos_builds not found

  4. 在网上一番搜索,先执行python3 -m pip install --user ohos-build

    image

  5. 但是之后也没有解决,于是换一种思路,直接更改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",
    ]
}
  1. 运行结果:

    image

posted @   ZzTzZ  阅读(235)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示