
回复
::: hljs-center
:::
Thread API分析:
osThreadId_t osThreadNew(osThreadFunc_t func, void *argument,const osThreadAttr_t *attr )
名字 | 描述 | |
---|---|---|
func | 线程函数. |
argumen | 作为启动参数传递给线程函数的指针 | |
---|---|---|
content2 | content3 |
void thread1(void)
{
int sum=0;
while (1)
{
/* code */
printf("This is BearPi-HM_Nano Thread1----%d\r\n",sum++);
usleep(1000000);
}
}
void thread2(void)
{
int sum=0;
while (1)
{
/* code */
printf("This is BearPi-HM_Nano Thread2----%d\r\n",sum++);
usleep(500000);
}
}
static void Thread_example(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*4;
attr.priority = 25;
if (osThreadNew((osThreadFunc_t)thread1, NULL, &attr) == NULL) {
printf("Falied to create thread1!\n");
}
attr.name = "thread2";
if (osThreadNew((osThreadFunc_t)thread2, NULL, &attr) == NULL) {
printf("Falied to create thread2!\n");
}
}
编译调试
修改 BUILD.gn 文件
修改 applications\BearPi\BearPi-HM_Nano\sample路径下 BUILD.gn 文件,指定 thread_example 参与编译。
“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”,
运行结果
示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志,Thread1和Thread2会交替打印信息
This is BearPi-HM_Nano Thread1----2
This is BearPi-HM_Nano Thread2----4
This is BearPi-HM_Nano Thread2----5
This is BearPi-HM_Nano Thread1----3
This is BearPi-HM_Nano Thread2----6
This is BearPi-HM_Nano Thread2----7