RT-Thread优先级问题,官方文档互斥量一节,线程2的优先级比线程1高,但在线程2running的时候还是会被线程1抢占,达不到官方文档的仿真运行结果。下图是我的仿真运行结果,输出打印的number都是有间隔的
代码都是官方文档代码,就改了优先级和线程2的while循环次数,代码如下:
#include <rtthread.h>
#define THREAD_PRIORITY 21
#define THREAD_TIMESLICE 5
/* 指向互斥量的指针 */
static rt_mutex_t dynamic_mutex = RT_NULL;
static rt_uint8_t number1,number2 = 0;
ALIGN(RT_ALIGN_SIZE)
static char thread1_stack[1024];
static struct rt_thread thread1;
static void rt_thread_entry1(void parameter)
{
while(1)
{
/ 线程 1 获取到互斥量后,先后对 number1、number2 进行加 1 操作,然后释放互斥量 */
//rt_kprintf("thread1.current_priority = %d\n",thread1.current_priority);
rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER);
//rt_kprintf("thread1.number1 = %d, mumber2 = %d \n",number1 ,number2);
number1++;
rt_thread_mdelay(10);
number2++;
rt_mutex_release(dynamic_mutex);
}
}
ALIGN(RT_ALIGN_SIZE)
static char thread2_stack[1024];
static struct rt_thread thread2;
static void rt_thread_entry2(void parameter)
{
while(1)
{
/ 线程 2 获取到互斥量后,检查 number1、number2 的值是否相同,相同则表示 mutex 起到了锁的作用 /
//rt_kprintf("thread2.current_priority = %d\n",thread2.current_priority);
//rt_kprintf("thread1.current_priority = %d\n",thread1.current_priority);
rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER);
if(number1 != number2)
{
rt_kprintf("not protect.number1 = %d, mumber2 = %d \n",number1 ,number2);
}
else
{
rt_kprintf("mutex protect ,number1 = mumber2 is %d\n",number1);
}
number1++;
number2++;
rt_mutex_release(dynamic_mutex);
if(number1>=10)
return;
}
}
/ 互斥量示例的初始化 /
int mutex_sample(void)
{
/ 创建一个动态互斥量 /
dynamic_mutex = rt_mutex_create("dmutex", RT_IPC_FLAG_PRIO);
if (dynamic_mutex == RT_NULL)
{
rt_kprintf("create dynamic mutex failed.\n");
return -1;
}
rt_thread_init(&thread1,
"thread1",
rt_thread_entry1,
RT_NULL,
&thread1_stack[0],
sizeof(thread1_stack),
THREAD_PRIORITY, THREAD_TIMESLICE);
rt_thread_startup(&thread1);
rt_thread_init(&thread2,
"thread2",
rt_thread_entry2,
RT_NULL,
&thread2_stack[0],
sizeof(thread2_stack),
THREAD_PRIORITY-1, THREAD_TIMESLICE);
rt_thread_startup(&thread2);
return 0;
}
/ 导出到 MSH 命令列表中 */
MSH_CMD_EXPORT(mutex_sample, mutex sample);
在代码仿真运行过程中也打印看了一下线程优先级,跟初始的线程优先级是一样的,没有变化,如下图: