RTT is a preemptible RTOS, and high-priority threads will execute it first.
This routine shows how to seize.
I am too lazy to write this explanation. The following section is from the official website forum:
Because of the higher priority, thread1 takes the lead to get the execution, and then it calls the delay. The time is three system tick, so thread2 gets the execution. You can find a rule in the printed results. After thread2 is printed twice and thread1 is printed once, then thread2 prints thread1 once every three times. By analyzing the entry program of two threads, we can find that thread2 will actually receive three execution opportunities in the latency of three tick systems in thread1, however, it is clear that the third execution of thread2 does not end in the first delay of thread1. After the third delay, thread2 should execute the third print count, however, because the latency of thread1 is also over at this time, and its priority is higher than that of thread2, so it preemptible the execution of thread2 and started to execute. When thread1 enters the latency again, the printing of the previously preemptible thread2 can continue, and after two system tick latencies and two print counts, after the third system tick is finished, the latency of thread1 is terminated, and thread1 is preemptible again for execution. Therefore, before thread1 is printed, thread2 executes Three print counts.
Program:
#include <rtthread.h>struct rt_thread thread1;struct rt_thread thread2;static rt_uint8_t thread1_stack[512];static rt_uint8_t thread2_stack[512];static void thread1_entry(void *parameter){ static rt_uint32_t count = 0; for (; count<5; count++) { rt_thread_delay(RT_TICK_PER_SECOND * 3); rt_kprintf("count = %d\n", count); }}static void thread2_entry(void *parameter){ rt_tick_t tick; rt_uint32_t i; for (i=0; i<15; i++) { tick = rt_tick_get(); rt_thread_delay(RT_TICK_PER_SECOND); rt_kprintf("tick = %d\n", tick); }}int rt_application_init(){ rt_err_t result; result = rt_thread_init(&thread1, "t1", thread1_entry, RT_NULL, &thread1_stack[0], sizeof(thread1_stack), 5, 5); if (result == RT_EOK) rt_thread_startup(&thread1); result = rt_thread_init(&thread2, "t2", thread2_entry, RT_NULL, &thread2_stack[0], sizeof(thread2_stack), 6, 5); if (result == RT_EOK) rt_thread_startup(&thread2); return 0;}/*@}*/
Output result:
\ | /- RT - Thread Operating System/ | \ 1.1.0 build Aug 10 20122006 - 2012 Copyright by rt-thread teamtick = 1tick = 1001count = 0tick = 2001tick = 3002tick = 4002count = 1tick = 5002tick = 6002tick = 7002count = 2tick = 8002tick = 9002tick = 10002count = 3tick = 11003tick = 12004tick = 13005count = 4tick = 14006