文章作者: 盧宜良
Thread 在 RTOS 裡面非常的好用,使用者不需用main裡輪詢的方式,各個Thread 獨立,讓多位使用者協做時可以更專注於開發,加速程式碼的整合。
Kernel API : Thread :
k_thread_create 創建兩個 Thread 與 main Thread,分別 k_sleep 不同時間,並運行結果。
程式部分重點:
/* thread test */
#define STACKSIZE 1000
char thread_stacks[2][STACKSIZE];
static struct k_thread threads[2];
void thread_1(void *arg1, void *arg2, void *arg3)
{
while (1) {
printk("thread_1\n");
k_sleep(600);
}
}
void thread_2(void *arg1, void *arg2, void *arg3)
{
while (1) {
printk("thread_2\n");
k_sleep(300);
}
}
void main(void)
{
console_init();
printk("Hello World! %s\n", CONFIG_ARCH);
k_thread_create(&threads[0], &thread_stacks[0][0], STACKSIZE,
thread_1, 0, 0, 0,
K_PRIO_COOP(4), 0, 0);
k_thread_create(&threads[1], &thread_stacks[1][0], STACKSIZE,
thread_2, 0, 0, 0,
K_PRIO_COOP(4), 0, 0);
while (1) {
printk("MAIN\n");
k_sleep(1000);
}
}
GoMCU![[原創] NXP i.MX8QXP dev kit notes tutorial 開發紀錄教學-GoMCU](https://imxdev.gitlab.io/images/imx8qxp-top.jpg)