你是如何启动thread1和thread2线程的?要注意线程的优先级。
举个例子,假设你的thread1和thread2的优先级是5,且在main线程中,创建两个线程,依次启动thread1和thread2,此时,只会有thread1运行,thread2是压根没有启动的,不可能从INIT状态变成就绪态,也就无法加入调度列表了。
如果你将thread1和thread2的优先级改成25,应该能从console看到thread1,2交替输出。
以下是我写的一段测试代码。
2023-04-26 12:39更新。我注意到你用的是Nano,代码已经更新,不使用rt_thread_create,去掉了Finsh部分,只使用rt_thread_init。
代码使用RT-Thread Nano 3.1.3,STM32L4321RCT6上测试。
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-04-25 lchnu first version
*/
#include
#define DBG_TAG \"main\"
#define DBG_LVL DBG_LOG
#include
#define THREAD_PRIORITY 5
#define THREAD_STACK_SIZE 2048
#define THREAD_TIMESLICE 5
static struct rt_thread thread1, thread2;
static rt_uint8_t thread1_stack[THREAD_STACK_SIZE];
static rt_uint8_t thread2_stack[THREAD_STACK_SIZE];
static void thread1_entry(void *parameter)
{
while(1)
{
rt_kprintf(\"Thread 1 is running\\n\");
}
}
static void thread2_entry(void *parameter)
{
while(1)
{
rt_kprintf(\"Thread 2 is running\\n\");
}
}
int main(void)
{
rt_err_t result;
result = rt_thread_init(&thread1,
\"thread1\",
thread1_entry,
RT_NULL,
&thread1_stack[0],
sizeof(thread1_stack),
THREAD_PRIORITY, THREAD_TIMESLICE);
if (result == RT_EOK)
{
rt_thread_startup(&thread1);
}
result = rt_thread_init(&thread2,
\"thread2\",
thread2_entry,
RT_NULL,
&thread2_stack[0],
sizeof(thread2_stack),
THREAD_PRIORITY, THREAD_TIMESLICE);
if (result == RT_EOK)
{
rt_thread_startup(&thread2);
}
return 0;
}
上述代码的运行结果,只会有thread1输出。
将上述代码的优先级改成25,如下图所示,是可以看到交替输出的。
你是如何启动thread1和thread2线程的?要注意线程的优先级。
举个例子,假设你的thread1和thread2的优先级是5,且在main线程中,创建两个线程,依次启动thread1和thread2,此时,只会有thread1运行,thread2是压根没有启动的,不可能从INIT状态变成就绪态,也就无法加入调度列表了。
如果你将thread1和thread2的优先级改成25,应该能从console看到thread1,2交替输出。
以下是我写的一段测试代码。
2023-04-26 12:39更新。我注意到你用的是Nano,代码已经更新,不使用rt_thread_create,去掉了Finsh部分,只使用rt_thread_init。
代码使用RT-Thread Nano 3.1.3,STM32L4321RCT6上测试。
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-04-25 lchnu first version
*/
#include
#define DBG_TAG \"main\"
#define DBG_LVL DBG_LOG
#include
#define THREAD_PRIORITY 5
#define THREAD_STACK_SIZE 2048
#define THREAD_TIMESLICE 5
static struct rt_thread thread1, thread2;
static rt_uint8_t thread1_stack[THREAD_STACK_SIZE];
static rt_uint8_t thread2_stack[THREAD_STACK_SIZE];
static void thread1_entry(void *parameter)
{
while(1)
{
rt_kprintf(\"Thread 1 is running\\n\");
}
}
static void thread2_entry(void *parameter)
{
while(1)
{
rt_kprintf(\"Thread 2 is running\\n\");
}
}
int main(void)
{
rt_err_t result;
result = rt_thread_init(&thread1,
\"thread1\",
thread1_entry,
RT_NULL,
&thread1_stack[0],
sizeof(thread1_stack),
THREAD_PRIORITY, THREAD_TIMESLICE);
if (result == RT_EOK)
{
rt_thread_startup(&thread1);
}
result = rt_thread_init(&thread2,
\"thread2\",
thread2_entry,
RT_NULL,
&thread2_stack[0],
sizeof(thread2_stack),
THREAD_PRIORITY, THREAD_TIMESLICE);
if (result == RT_EOK)
{
rt_thread_startup(&thread2);
}
return 0;
}
上述代码的运行结果,只会有thread1输出。
将上述代码的优先级改成25,如下图所示,是可以看到交替输出的。
举报