在RT-Thread中设计了thread1. thread2.thread3三个线程,优先级分别为9.10、11,设计了-个互斥量mutex. 线程thread1优先级最高.先执行100ms延时.之后再打印线程⒉与线程3的优先级信息—用于检查线程thread3的优先级是否被提升为thread2的优先级. 线程thread2进入后先打印自己的优先级.然后进入 50ms延时,延时结束后获取互斥量mutex,获取到互斥 量之后再释放互斥量mutex. 线程thread3进入后先打印自己的优先级,然后获取互斥量mutex,获取到互斥量之后进入500ms的循环.循环结束后将互斥量释放. 要求设置一个32bit的全局变量来对三个线程分别进行标记,以此在逻辑分析中可以观察这三个线程的运行状态,要求条理清晰
时间: 2023-08-16 21:09:16 浏览: 80
本题中涉及到了三个线程和一个互斥量,需要注意线程优先级和互斥量的使用。同时需要设置一个全局变量来对三个线程进行标记,以便观察它们的运行状态。
首先,我们需要定义三个线程和一个互斥量:
```c
static rt_thread_t thread1 = RT_NULL;
static rt_thread_t thread2 = RT_NULL;
static rt_thread_t thread3 = RT_NULL;
static rt_mutex_t mutex;
```
然后,我们需要设置三个线程的优先级,创建并启动这三个线程:
```c
static char thread1_stack[1024];
static void thread1_entry(void* parameter)
{
rt_thread_delay(100);
rt_kprintf("Thread2 priority is %d, Thread3 priority is %d\n", rt_thread_get_priority(thread2), rt_thread_get_priority(thread3));
}
static char thread2_stack[1024];
static void thread2_entry(void* parameter)
{
rt_kprintf("Thread2 priority is %d\n", rt_thread_get_priority(thread2));
rt_thread_delay(50);
rt_mutex_take(&mutex, RT_WAITING_FOREVER);
rt_mutex_release(&mutex);
}
static char thread3_stack[1024];
static void thread3_entry(void* parameter)
{
rt_kprintf("Thread3 priority is %d\n", rt_thread_get_priority(thread3));
rt_mutex_take(&mutex, RT_WAITING_FOREVER);
for (int i = 0; i < 500; ++i) {
rt_thread_delay(1);
}
rt_mutex_release(&mutex);
}
void thread_init()
{
rt_err_t result;
result = rt_mutex_init(&mutex, "mutex", RT_IPC_FLAG_FIFO);
RT_ASSERT(result == RT_EOK);
thread1 = rt_thread_create("thread1", thread1_entry, RT_NULL, sizeof(thread1_stack), 9, 20);
RT_ASSERT(thread1 != RT_NULL);
result = rt_thread_startup(thread1);
RT_ASSERT(result == RT_EOK);
thread2 = rt_thread_create("thread2", thread2_entry, RT_NULL, sizeof(thread2_stack), 10, 20);
RT_ASSERT(thread2 != RT_NULL);
result = rt_thread_startup(thread2);
RT_ASSERT(result == RT_EOK);
thread3 = rt_thread_create("thread3", thread3_entry, RT_NULL, sizeof(thread3_stack), 11, 20);
RT_ASSERT(thread3 != RT_NULL);
result = rt_thread_startup(thread3);
RT_ASSERT(result == RT_EOK);
}
```
最后,我们需要设置一个全局变量来对三个线程进行标记,以便观察它们的运行状态:
```c
static uint32_t thread_flags = 0;
#define THREAD1_FLAG (1 << 0)
#define THREAD2_FLAG (1 << 1)
#define THREAD3_FLAG (1 << 2)
#define SET_THREAD_FLAG(flag) do { \
rt_enter_critical(); \
thread_flags |= (flag); \
rt_exit_critical(); \
} while (0)
#define CLEAR_THREAD_FLAG(flag) do { \
rt_enter_critical(); \
thread_flags &= ~(flag); \
rt_exit_critical(); \
} while (0)
```
在每个线程的入口函数中,我们需要在适当的位置设置标志位:
```c
static char thread1_stack[1024];
static void thread1_entry(void* parameter)
{
rt_thread_delay(100);
rt_kprintf("Thread2 priority is %d, Thread3 priority is %d\n", rt_thread_get_priority(thread2), rt_thread_get_priority(thread3));
SET_THREAD_FLAG(THREAD1_FLAG);
}
static char thread2_stack[1024];
static void thread2_entry(void* parameter)
{
rt_kprintf("Thread2 priority is %d\n", rt_thread_get_priority(thread2));
rt_thread_delay(50);
rt_mutex_take(&mutex, RT_WAITING_FOREVER);
rt_mutex_release(&mutex);
SET_THREAD_FLAG(THREAD2_FLAG);
}
static char thread3_stack[1024];
static void thread3_entry(void* parameter)
{
rt_kprintf("Thread3 priority is %d\n", rt_thread_get_priority(thread3));
rt_mutex_take(&mutex, RT_WAITING_FOREVER);
for (int i = 0; i < 500; ++i) {
rt_thread_delay(1);
}
rt_mutex_release(&mutex);
SET_THREAD_FLAG(THREAD3_FLAG);
}
```
可以在主函数中添加一个循环,不断检查标志位的值,以观察三个线程的运行状态:
```c
int main(void)
{
thread_init();
while (1) {
rt_kprintf("Thread1 %s running\n", (thread_flags & THREAD1_FLAG) ? "is" : "is not");
rt_kprintf("Thread2 %s running\n", (thread_flags & THREAD2_FLAG) ? "is" : "is not");
rt_kprintf("Thread3 %s running\n", (thread_flags & THREAD3_FLAG) ? "is" : "is not");
rt_thread_delay(1000);
}
return 0;
}
```
阅读全文