HarmonyOS小熊派 | HarmonyOS内核编程开发—Thread多线程

举报
Yuchuan 发表于 2021/06/25 11:05:08 2021/06/25
【摘要】 函数osThreadNew通过将线程添加到活动线程列表并将其设置为就绪状态来启动线程函数。线程函数的参数使用参数指针*argument传递。当创建的thread函数的优先级高于当前运行的线程时,创建的thread函数立即启动并成为新的运行线程。线程属性是用参数指针attr定义的。属性包括线程优先级、堆栈大小或内存分配的设置。可以在RTOS启动(调用 osKernelStart)之前安全地调用该函数

BearPi-HM_Nano开发板HarmonyOS内核编程开发——Thread多线程

本示例将演示如何在BearPi-HM_Nano开发板上使用cmsis 2.0 接口进行多线程开发

Thread API分析

osThreadNew()

osThreadId_t osThreadNew(osThreadFunc_t	func, void *argument,const osThreadAttr_t *attr )	

描述:

函数osThreadNew通过将线程添加到活动线程列表并将其设置为就绪状态来启动线程函数。线程函数的参数使用参数指针*argument传递。当创建的thread函数的优先级高于当前运行的线程时,创建的thread函数立即启动并成为新的运行线程。线程属性是用参数指针attr定义的。属性包括线程优先级、堆栈大小或内存分配的设置。可以在RTOS启动(调用 osKernelStart)之前安全地调用该函数,但不能在内核初始化 (调用 osKernelInitialize)之前调用该函数。

注意 :不能在中断服务调用该函数

参数:

名字 描述
func 线程函数.
argument 作为启动参数传递给线程函数的指针
attr 线程属性

软件设计

主要代码分析

在Thread_example函数中,通过osThreadNew()函数创建了thread1和thread2两个进程,thread1和thread2启动后会输出打印日志。

applications\BearPi\BearPi-HM_Nano\sample\kernal_task\task_example.c

task_example.c

#include <stdio.h>
#include <unistd.h>
#include "cmsis_os2.h"
#include "ohos_init.h"

void taskThreadOne(void)
{
    int sum = 0;
    while (1)
    {
        /* code */
        printf("This is YuchuanHuaying Create TaskThreadOne---%d\n",sum++);
        usleep(1000000);
    }
    
}

void taskThreadTwo(void)
{
    int sum = 0;
    while (1)
    {
        /* code */
        printf("This is YuchuanHuaying Create TaskThreadTwo---%d\n",sum++);
        usleep(500000);
    }
    
}

static void taskExample(void)
{
    osThreadAttr_t attr;

    attr.name = "task_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)taskThreadOne,NULL,&attr) == NULL)
    {
        /* code */
        printf("Fialed to Create Task_1 !!!\n");
    }
    
    attr.name = "task_2";

    if (osThreadNew((osThreadFunc_t)taskThreadTwo,NULL,&attr) == NULL)
    {
        /* code */
        printf("Fialed to Create Task_2 !!!\n");
    }
    
}

APP_FEATURE_INIT(taskExample);

applications\BearPi\BearPi-HM_Nano\sample\kernal_task\BUILD.gn

static_library("task_example"){
    sources = [
        "task_example.c",
    ]

    include_dirs = [
        "//utils/native/lite/include",
        "//kernel/liteos_m/components/cmsis/2.0",
    ]
}

applications\BearPi\BearPi-HM_Nano\sample\BUILD.gn

# Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build/lite/config/component/lite_component.gni")

lite_component("app") {
    features = [
        #"A1_kernal_thread:thread_example",
        #"A2_kernel_timer:timer_example",
        #"A3_kernel_event:event_example",
        #"A4_kernel_mutex:mutex_example",
        #"A5_kernel_semaphore:semaphore_example",
        #"A6_kernel_message:message_example",

        #"B1_basic_led_blink:led_example",
        #"B2_basic_button:button_example",
        #"B3_basic_pwm_led:pwm_example",
        #"B4_basic_adc:adc_example",
        #"B5_basic_i2c_nfc:i2c_example",
        #"B6_basic_uart:uart_example",
        
        #"C1_e53_sf1_mq2:e53_sf1_example",
        #"C2_e53_ia1_temp_humi_pls:e53_ia1_example",
        #"C3_e53_sc1_pls:e53_sc1_example",
        #"C4_e53_sc2_axis:e53_sc2_example",
        #"C5_e53_is1_infrared:e53_is1_example",

        #"D1_iot_wifi_ap:wifi_ap",
        #"D2_iot_wifi_sta_connect:wifi_sta_connect",        
        #"D3_iot_udp_client:udp_client",
        #"D4_iot_tcp_server:tcp_server",
        #"D5_iot_mqtt:iot_mqtt",        
        #"D6_iot_cloud_oc:oc_mqtt",
        #"D7_iot_cloud_onenet:onenet_mqtt",
        #"D8_iot_cloud_oc_smoke:cloud_oc_smoke",
        #"D9_iot_cloud_oc_light:cloud_oc_light",
        #"D10_iot_cloud_oc_manhole_cover:cloud_oc_manhole_cover",
        #"D11_iot_cloud_oc_infrared:cloud_oc_infrared",
        #"D12_iot_cloud_oc_agriculture:cloud_oc_agriculture",
        #"D13_iot_cloud_oc_gps:cloud_oc_gps",
        "kernal_task:task_example",
    ]
}

运行结果

示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志,Thread1和Thread2会交替打印信息

ready to OS start
sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
formatting spiffs...
FileSystem mount ok.
wifi init success!

This is YuchuanHuaying Create TaskThreadOne---0
This is YuchuanHuaying Create TaskThreadTwo---0
00 00:00:00 0 196 D 0/HIVIEW: hilog init success.
00 00:00:00 0 196 D 0/HIVIEW: log limit init success.
00 00:00:00 0 196 I 1/SAMGR: Bootstrap core services(count:3).
00 00:00:00 0 196 I 1/SAMGR: Init service:0x4ae90c TaskPool:0xfa224
00 00:00:00 0 196 I 1/SAMGR: Init service:0x4ae930 TaskPool:0xfa894
00 00:00:00 0 196 I 1/SAMGR: Init service:0x4aea40 TaskPool:0xfaa54
00 00:00:00 0 228 I 1/SAMGR: Init service 0x4ae930 <time: 0ms> success!
00 00:00:00 0 128 I 1/SAMGR: Init service 0x4ae90c <time: 0ms> success!
00 00:00:00 0 72 D 0/HIVIEW: hiview init success.
00 00:00:00 0 72 I 1/SAMGR: Init service 0x4aea40 <time: 0ms> success!
00 00:00:00 0 72 I 1/SAMGR: Initialized all core system services!
00 00:00:00 0 128 I 1/SAMGR: Bootstrap system and application services(count:0).
00 00:00:00 0 128 I 1/SAMGR: Initialized all system and application services!
00 00:00:00 0 128 I 1/SAMGR: Bootstrap dynamic registered services(count:0).
This is YuchuanHuaying Create TaskThreadTwo---1
This is YuchuanHuaying Create TaskThreadOne---1
This is YuchuanHuaying Create TaskThreadTwo---2
This is YuchuanHuaying Create TaskThreadTwo---3
This is YuchuanHuaying Create TaskThreadOne---2
This is YuchuanHuaying Create TaskThreadTwo---4
This is YuchuanHuaying Create TaskThreadTwo---5
This is YuchuanHuaying Create TaskThreadOne---3
This is YuchuanHuaying Create TaskThreadTwo---6
This is YuchuanHuaying Create TaskThreadTwo---7
This is YuchuanHuaying Create TaskThreadOne---4
This is YuchuanHuaying Create TaskThreadTwo---8
This is YuchuanHuaying Create TaskThreadTwo---9
This is YuchuanHuaying Create TaskThreadOne---5
This is YuchuanHuaying Create TaskThreadTwo---10
This is YuchuanHuaying Create TaskThreadTwo---11
This is YuchuanHuaying Create TaskThreadOne---6
This is YuchuanHuaying Create TaskThreadTwo---12
This is YuchuanHuaying Create TaskThreadTwo---13
This is YuchuanHuaying Create TaskThreadOne---7
This is YuchuanHuaying Create TaskThreadTwo---14
This is YuchuanHuaying Create TaskThreadTwo---15
This is YuchuanHuaying Create TaskThreadOne---8
This is YuchuanHuaying Create TaskThreadTwo---16
This is YuchuanHuaying Create TaskThreadTwo---17
This is YuchuanHuaying Create TaskThreadOne---9
This is YuchuanHuaying Create TaskThreadTwo---18
This is YuchuanHuaying Create TaskThreadTwo---19
This is YuchuanHuaying Create TaskThreadOne---10
This is YuchuanHuaying Create TaskThreadTwo---20
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。