1
在Niobe WIFI IoT开发板上使用smsis 2.0 接口进行多线程开发
osThreadId_t osThreadNew(osThreadFunc_tfunc, void *argument,const osThreadAttr_t *attr )
描述:
线程函数
参数:
名字 | 描述 |
---|---|
func | 线程函数. |
argument | 参数传递指针 |
attr | 线程属性 |
主要代码分析
在OS_Thread_example函数中,通过osThreadNew()函数创建了thread1和thread2两个进程,thread1和thread2启动后会输出打印日志。
void thread_entry1(void)
{
int sum=0;
while (1)
{
/* 示例代码 */
printf("This is Niobe Thread1----%d\r\n",sum++);
usleep(500000);
}
}
void thread_entry2(void)
{
int sum=0;
while (1)
{
/* 示例代码 */
printf("This is Niobe Thread2----%d\r\n",sum++);
usleep(500000);
}
}
static void OS_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)thread_entry1, NULL, &attr) == NULL) {
printf("Falied to create thread1!\n");
}
attr.name = "thread2";
if (osThreadNew((osThreadFunc_t)thread_entry2, NULL, &attr) == NULL) {
printf("Falied to create thread2!\n");
}
}
编译调试
修改 BUILD.gn 文件
修改 applications\app路径下 BUILD.gn 文件,指定 os_thread_example 参与编译。
#"TW001_OS_helloworld:helloworld_example",
"TW002_OS_thread:os_thread_example",
#"TW003_OS_timer:os_timer_example",
#"TW004_OS_event:os_event_example",
#"TW005_OS_mutex:os_mutex_example",
#"TW006_OS_semp:os_semaphore_example",
#"TW007_OS_message:os_message_example",
示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志,Thread1和Thread2会交替打印信息,显示如下
This is Niobe Thread1----2
This is Niobe Thread2----5
This is Niobe Thread1----3
This is Niobe Thread2----6