初级会员
- 积分
- 80
- 金钱
- 80
- 注册时间
- 2020-5-9
- 在线时间
- 13 小时
|
1金钱
int main(void)
{
delay_init(168);
MX_USART1_UART_Init();
freertos_init();
}
static void thread1_entry(void *parameter)
{
rt_uint32_t count = 0;
while(1)
{
rt_kprintf("thread 1 count = %d\r\n",count++);
rt_thread_yield();
}
}
static void thread2_entry(void *parameter)
{
rt_uint32_t count = 0;
while(1)
{
rt_kprintf("thread 2 count = %d\r\n",count++);
rt_thread_yield();
}
}
static void startDefaultTaskFuc(void *parameter)
{
tid2 = rt_thread_create("t1", thread1_entry, RT_NULL, 512, 5, 5);
if(tid2!=RT_NULL)
rt_thread_startup(tid2);
else
{
rt_kprintf("create thread1 failed!\r\n");
}
tid3 = rt_thread_create("t2", thread2_entry, RT_NULL, 512, 5, 5);
if(tid3 !=RT_NULL)
{
rt_thread_startup(tid3);
}
else
{
rt_kprintf("create thread2 failed!\r\n");
}
}
void freertos_init(void)
{
rt_thread_init(&startDefaultTask, "start", startDefaultTaskFuc, RT_NULL, rt_startDefaultTask_stack, sizeof(rt_startDefaultTask_stack), 3, 20);
rt_thread_startup(&startDefaultTask);
}
以上程序,理论上应该是线程1和线程2交替运行吧,为什么我的rt_thread_yield好像不起作用 ,rt_thread_yield函数中该处if (to_thread != rt_current_thread)判断错误。在打印函数还未执行完就跳到另一个线程了
收←◆thread 1 count = 0
thread 1 count = 1
thread 1 count = 2
thread 1 count = 3
thread 1 count = thread 2 count = 0
thread 2 count = 1
thread 2 c2
thread 1 count = 5
thread 1 count = 6
threadount = 7
thread 2 count = 3
thread 2 count = 4
2 count = 4
thread 1 count = 8
thread 1 count =thread 2 count = 5
thread 2 count = 6
thread 2 c 7
thread 1 count = 10
thread 1 count = 11
thrount = 12thread 2 count = 8
thread 2 count = 9
ead 2 count = 9
\0thread 1 count = 13
|
|