(一)鸿蒙设备应用开发笔记——创建线程
#include <stdio.h>#include <string.h>#include <unistd.h>#include “ohos_init.h”#include “cmsis_os2.h”static void threadTest(void){osThreadAttr_t attr;attr.name = “thread1”;attr.attr_b
·
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include “ohos_init.h”
#include “cmsis_os2.h”
void thread1_func(void) //线程1执行的功能函数
{
while(1)
{
printf(“hello\n”);
sleep(1);
}
}
void thread2_func(void) //线程2执行的功能函数
{
while(1)
{
printf(“harmony\n”);
sleep(1);
}
}
static void threadTest(void)
{
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;
//创建线程,并且判断是否创建成功
if(osThreadNew((osThreadFunc_t)thread1_func, NULL, &attr) == NULL)
{
printf(“failed to create thread1\n”);
}
//创建线程2,修改线程名称,可以共用同一个线程对象的其他属性
attr.name = “thread2”;
if(osThreadNew((osThreadFunc_t)thread2_func, NULL, &attr) == NULL)
{
printf(“failed to create thread2\n”);
}
}
更多推荐
已为社区贡献5条内容
所有评论(0)