粤嵌鸿蒙 -- 学习笔记
基本介绍任务创建
内核篇 : 1.Thread多线程 2.定时器 3.事件
拓展
static_library("hello")
{ sources = [ "hello.c", ] }
#include "ohos_init.h"
#include
void hello(void)
{ printf("hello,openharmony!!!\n"); }
APP_FEATURE_INIT(hello);
#include "ohos_init.h"头文件包含:APP_FEATURE_INIT函数
#include
osThreadId_t osThreadNew(osThreadFunc_t func, void *argument, const osThreadAttr_t *attr)
osThreadAttr_t attr; //指定线程的属性
attr.name = "thread1";
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)thread1, NULL, &attr) == NULL) //创建线程 -- 后面编写线程函数thread1
{ printf("Falied to create thread1!\n"); }
osTimerId_t osTimerNew(osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr)
osTimerId_t id = osTimerNew(callback, osTimerPeriodic, NULL, NULL);
或
osTimerId_t id1; id1 = osTimerNew(Timer1_Callback, osTimerPeriodic, &exec1, NULL);
osStatus_t osTimerStart(osTimerId_t timer_id, uint32_t ticks)
注意:不能再终端服务调用函数
// Hi3861 1U=10ms,100U=1S
timerDelay = 100U;
status = osTimerStart(id1, timerDelay);
if (status != osOK)
{ // Timer could not be started }
osStatus_t osTimerStop(osTimerId_t timer_id) 参数:创建的定时器的ID
osStatus_t osTimerDelete(osTimerId_t timer_id) 参数:创建的定时器的ID
osEventFlagsId_t osEventFlagsNew(const osEventFlagsAttr_t *attr) 注意:不能在中断服务函数中调用该函数
evt_id = osEventFlagsNew(NULL); //attr为事件标志属性;空:默认值.
uint32_t osEventFlagsSet(osEventFlagsId_t ef_id, uint32_t flags) 注意:不能在中断服务函数中调用该函数
#define FLAGS_MSK1 0x00000001U
osEventFlagsSet(evt_id,FLAGS_MSK1); osThreadYield(); -----> 挂起线程(可以不写)
uint32_t osEventFlagsWait(osEventFlagsId_t ef_id, uint32_t flags, uint32_t options, uint32_t timeout)
注意:不能在中断服务函数中调用该函数
osEventFlagsWait(evt_id,FLAGS_MSK1,osFlagsWaitAny,osWaitForever); //等待 FLAGS_MSK1 事件
// 统一对创建新的线程进行封装
osThreadId_t newThread(char *name, osThreadFunc_t func, void *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("osThreadNew(%s) failed.\r\n", name);
else
printf("osThreadNew(%s) success, thread id: %d.\r\n", name, tid);
return tid;
}
// 创建线程
osThreadId_t t1 = newThread("feedCat", feedCat, NULL);
osThreadId_t t2 = newThread("feedDog", feedDog, NULL);