array(2) { ["docs"]=> array(10) { [0]=> array(10) { ["id"]=> string(3) "428" ["text"]=> string(77) "Visual Studio 2017 单独启动MSDN帮助(Microsoft Help Viewer)的方法" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(8) "DonetRen" ["tagsname"]=> string(55) "Visual Studio 2017|MSDN帮助|C#程序|.NET|Help Viewer" ["tagsid"]=> string(23) "[401,402,403,"300",404]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400964" ["_id"]=> string(3) "428" } [1]=> array(10) { ["id"]=> string(3) "427" ["text"]=> string(42) "npm -v;报错 cannot find module "wrapp"" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(4) "zzty" ["tagsname"]=> string(50) "node.js|npm|cannot find module "wrapp“|node" ["tagsid"]=> string(19) "[398,"239",399,400]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400760" ["_id"]=> string(3) "427" } [2]=> array(10) { ["id"]=> string(3) "426" ["text"]=> string(54) "说说css中pt、px、em、rem都扮演了什么角色" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(12) "zhengqiaoyin" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400640" ["_id"]=> string(3) "426" } [3]=> array(10) { ["id"]=> string(3) "425" ["text"]=> string(83) "深入学习JS执行--创建执行上下文(变量对象,作用域链,this)" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "Ry-yuan" ["tagsname"]=> string(33) "Javascript|Javascript执行过程" ["tagsid"]=> string(13) "["169","191"]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511399901" ["_id"]=> string(3) "425" } [4]=> array(10) { ["id"]=> string(3) "424" ["text"]=> string(30) "C# 排序技术研究与对比" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(9) "vveiliang" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(8) ".Net Dev" ["catesid"]=> string(5) "[199]" ["createtime"]=> string(10) "1511399150" ["_id"]=> string(3) "424" } [5]=> array(10) { ["id"]=> string(3) "423" ["text"]=> string(72) "【算法】小白的算法笔记:快速排序算法的编码和优化" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(9) "penghuwan" ["tagsname"]=> string(6) "算法" ["tagsid"]=> string(7) "["344"]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511398109" ["_id"]=> string(3) "423" } [6]=> array(10) { ["id"]=> string(3) "422" ["text"]=> string(64) "JavaScript数据可视化编程学习(二)Flotr2,雷达图" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "chengxs" ["tagsname"]=> string(28) "数据可视化|前端学习" ["tagsid"]=> string(9) "[396,397]" ["catesname"]=> string(18) "前端基本知识" ["catesid"]=> string(5) "[198]" ["createtime"]=> string(10) "1511397800" ["_id"]=> string(3) "422" } [7]=> array(10) { ["id"]=> string(3) "421" ["text"]=> string(36) "C#表达式目录树(Expression)" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(4) "wwym" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(4) ".NET" ["catesid"]=> string(7) "["119"]" ["createtime"]=> string(10) "1511397474" ["_id"]=> string(3) "421" } [8]=> array(10) { ["id"]=> string(3) "420" ["text"]=> string(47) "数据结构 队列_队列实例:事件处理" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "idreamo" ["tagsname"]=> string(40) "C语言|数据结构|队列|事件处理" ["tagsid"]=> string(23) "["246","247","248",395]" ["catesname"]=> string(12) "数据结构" ["catesid"]=> string(7) "["133"]" ["createtime"]=> string(10) "1511397279" ["_id"]=> string(3) "420" } [9]=> array(10) { ["id"]=> string(3) "419" ["text"]=> string(47) "久等了,博客园官方Android客户端发布" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(3) "cmt" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511396549" ["_id"]=> string(3) "419" } } ["count"]=> int(200) } 222 基于小熊派Hi3861鸿蒙开发的IoT物联网学习【二】 - 爱码网
1314520xh
HarmonyOS内核开发—信号量开发案例学习记录
 
一、LiteOS里面的任务管理介绍:
任务状态通常分为以下四种:
就绪(Ready):该任务在就绪列表中,只等待CPU。
运行(Running):该任务正在执行。
阻塞(Blocked):该任务不在就绪列表中。包含任务被挂起、任务被延时、任务正在等待信号量、读写队列或者等待读写事件等。
退出态(Dead):该任务运行结束,等待系统回收资源。

案例 :cmsis_os2的API任务接口

创建任务:osThreadNew(osThreadFunc_t func,void * argument,const osThreadAttr_t * attr)
删除某个任务:osThreadTerminate(osThreadId_t thread_id);
任务挂起:osThreadSuspend(osThreadId_t thread_id)
任务恢复:osThreadResume (osThreadId_t thread_id)

 

 

 

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

osThreadId_t threadHiID;
osThreadId_t threadLoID;

/*****任务一*****/
void threadHi(void)
{
    // int sum = 0;
    // while (1)
    // {
    //     printf("This is BearPi Harmony Thread1----%d\r\n", sum++);
    //     usleep(1000000);
    // }
    printf("enter threadHi\r\n");
    osDelay(1);   //其作用是让任务阻塞
    printf("threadHi delay done\r\n");
    osThreadSuspend(threadHiID); //任务挂起
    printf("threadHi osThreadResume success\r\n");
    osThreadTerminate(threadHiID); //删除某个任务
    
}

/*****任务二*****/
void threadLo(void)
{
    // int sum = 0;
    // while (1)
    // {
    //     printf("This is BearPi Harmony Thread2----%d\r\n", sum++);
    //     usleep(500000);
    // }
    for (int i = 0;i<10;i++){
        printf("enter threadLo\r\n");
    }
    printf("threadHi osThreadSuspend success\r\n");
    osThreadResume(threadHiID);    // 任务恢复
    osThreadTerminate(threadLoID); //删除某个任务
}

/*****任务创建*****/
static void Thread_example(void)
{
    osThreadAttr_t attr;


    threadHiID = osThreadNew((osThreadFunc_t)threadHi,NULL,&attr);
    if (threadHiID == NULL)
    {
        printf("Falied to create threadHi!\n");
    }

    attr.name = "threadLo";
    attr.priority = 24 ;
    threadLoID =osThreadNew((osThreadFunc_t)threadLo,NULL,&attr);

    if (threadLoID== NULL)
    {
        printf("Falied to create threadLo!\n");
    }
}

APP_FEATURE_INIT(Thread_example);
// SYS_RUN(Thread_example);

 

二、信号量开发案例
基本概念:信号量(Semaphore)是一种实现任务间通信的机制,实现任务之间同步或临界资源的互斥访问。常用于协助一组相
互竞争的任务来访问临界资源。
1、在多任务系统中,各任务之间需要同步或互斥实现临界资源的保护,信号量功能可以为用户提供这方面的支持。
 
2、通常一个信号量的计数值用于对应有效的资源数,表示剩下的可被占用的互斥资源数。其值的含义分两种情况:
      1)0,表示没有积累下来的Post信号量操作,且有可能有在此信号量上阻塞的任务。
      2)正值,表示有一个或多个Post信号量操作。
 
4、以同步为目的的信号量和以互斥为目的的信号量在使用有如下不同:
     1)用作互斥时,信号量创建后记数是满的,在需要使用临界资源时,先取信号量,使其变空,这样其他任务需要使用
          临界资源时就会因为无法取到信号量而阻塞,从而保证了临界资源的安全。
 
     2)用作同步时,信号量在创建后被置为空,任务1取信号量而阻塞,任务2在某种条件发生后,释放信号量,于是任务
         1得以进入READYRUNNING态,从而达到了两个任务间的同步。
 
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

osSemaphoreId_t sem1;

void Thread_Semaphore1(void)
{
    osStatus_t status;
    while (1)
    {
        //申请两次sem1信号量,使得Thread_Semaphore2和Thread_Semaphore3能同步执行
        status = osSemaphoreRelease(sem1);
        if (status!=osOK){
            printf("semaphore   fail");
        }else{
            printf("semaphore   success");
        }

        //此处若只申请一次信号量,则Thread_Semaphore2和Thread_Semaphore3会交替运行。
        // osSemaphoreRelease(sem1);

        // printf("Thread_Semaphore i %d \n",i);
        // i=i+1;
        // printf("Thread_Semaphore sem1 %d \n",sem1);
        // printf("Thread_Semaphore1 Release  Semap \n");
        osDelay(100);
    }
}
void Thread_Semaphore2(void)
{
    osStatus_t status;
    while (1)
    {
        //等待sem1信号量
        status = osSemaphoreAcquire(sem1, 50U);
        if (status!=osOK){
            printf("semaphore2   fail");
        }else{
            printf("semaphore2   success");
        }
    }
}

void Thread_Semaphore3(void)
{
    osStatus_t status;
    while (1)
    {
        //等待sem1信号量
        status = osSemaphoreAcquire(sem1, osWaitForever);
        if (status!=osOK){
            printf("semaphore3 fail");
        }else{
            printf("semaphore3   success");
        }
        osDelay(1);
    }
}

void Semaphore_example(void)
{
    osThreadAttr_t attr;

    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024 * 4;
    attr.priority = 24;

    attr.name = "Thread_Semaphore1";
    if (osThreadNew((osThreadFunc_t)Thread_Semaphore1, NULL, &attr) == NULL)
    {
        printf("Falied to create Thread_Semaphore1!\n");
    }
    attr.name = "Thread_Semaphore2";
    if (osThreadNew((osThreadFunc_t)Thread_Semaphore2, NULL, &attr) == NULL)
    {
        printf("Falied to create Thread_Semaphore2!\n");
    }
    attr.name = "Thread_Semaphore3";
    if (osThreadNew((osThreadFunc_t)Thread_Semaphore3, NULL, &attr) == NULL)
    {
        printf("Falied to create Thread_Semaphore3!\n");
    }
    sem1 = osSemaphoreNew(4, 0, NULL);
    if (sem1 == NULL)
    {
        printf("Falied to create Semaphore1!\n");
    }
}
APP_FEATURE_INIT(Semaphore_example);
BUILD.gn文件:

static_library("semaphore_example") {
    sources = [
        "Semaphore_example.c"     
    ]

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

 

BUILD.gn

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

lite_component("app") {
    features = [
        "A5_kernel_semaphore:semaphore_example"
    ]
}

 

 

代码写完后用hpm dist命令来编译

 

 烧录代码到开发板:

 

后台log

 

 

 

 

 

 

 

 

 

分类:

技术点:

相关文章: