高优先级线程有可能造成低优先级线程饥饿,所以一个良好的工程实践是:把高优先级线程里所有非紧急任务下放给低优先级线程执行。
在本练习中,我们将创建并初始化一个工作队列,用于承接来自高优先级线程的任务。
1. 在本课程的 GitHub 仓库中,打开此练习的基础代码,位于 l7/7_e3.
具有不同优先级的线程
2. 定义本练习用到的三个线程优先级,要求 thread0 的优先级高于 thread1。工作队列线程优先级设为最低,因为我们希望由它来执行转交过来的非紧急任务。记住:优先级越高,数值越小。
#define THREAD0_PRIORITY 2
#define THREAD1_PRIORITY 3
#define WORKQ_PRIORITY 4C3. 代码中已经给出了 thread0。它会初始化内部数据结构 time_stamp 和 delta_time;随后在 while 循环里调用内核函数 k_uptime_get() 获取时间戳,接着模拟一段任务处理,再通过 k_uptime_delta() 获取并打印本轮任务的耗时。之后休眠 20 毫秒,循环往复一直运行。
void thread0(void)
{
uint64_t time_stamp;
int64_t delta_time;
while (1) {
time_stamp = k_uptime_get();
emulate_work();
delta_time = k_uptime_delta(&time_stamp);
printk("thread0 yielding this round in %lld ms\n", delta_time);
k_msleep(20);
}
}C4. thread1 执行完全相同的操作。为 thread1 添加以下代码:
void thread1(void)
{
uint64_t time_stamp;
int64_t delta_time;
while (1) {
time_stamp = k_uptime_get();
emulate_work();
delta_time = k_uptime_delta(&time_stamp);
printk("thread1 yielding this round in %lld ms\n", delta_time);
k_msleep(20);
}
}C请注意,由于该线程优先级较低,它处理 emulate_work() 的时间会更少。
5. 在线程入口函数之前,定义一个内联函数用来模拟任务:该函数内部跑循环,不主动让出 CPU,也不进入休眠。
static inline void emulate_work()
{
for(volatile int count_out = 0; count_out < 300000; count_out ++);
}C在运行频率为 128 MHz 的 nRF54L15 SoC 上,此函数大约需要 ~24 毫秒完成。在 64 MHz 的 nRF52840 SoC 上,所需时间应翻倍。
6. 构建应用程序并将其烧录到您的开发套件中。使用串口终端,您现在应该看到以下输出:
*** Booting nRF Connect SDK ***
*** Using Zephyr OS ***
thread0 yielding this round in 23 ms
thread0 yielding this round in 24 ms
thread1 yielding this round in 50 ms
thread0 yielding this round in 23 ms
thread0 yielding this round in 23 ms
thread1 yielding this round in 50 ms
thread0 yielding this round in 24 ms
thread0 yielding this round in 23 ms
thread1 yielding this round in 50 ms
thread0 yielding this round in 23 msTerminal可以看到,高优先级的 thread0 执行 emulate_work 任务大约耗时 23~26 毫秒,但 thread1 的耗时超过两倍。原因就是 thread0 持续阻塞 thread1。
线程运行时序大致如下:

从高优先级任务卸载工作
thread0 执行的是非紧急任务,仅仅为了执行这类任务就阻塞其他线程,并不是良好的工程做法。我们把非紧急的 emulate_work() 交给优先级更低的工作队列线程来处理。
7. 我们需要把任务(emulate_work())封装成一个工作项,并投递到指定工作队列。实现方式是定义 work_info 结构体,再编写 offload_function(),该函数内部只执行 emulate_work()。
struct work_info {
struct k_work work;
char name[25];
} my_work;
void offload_function(struct k_work *work_term)
{
emulate_work();
}C8. 在 thread0 的入口函数中,使用 k_work_queue_start() 启动工作队列。然后使用 k_work_init() 初始化工作项,将工作项与其处理程序 offload_function() 关联起来。
k_work_queue_start(&offload_work_q, my_stack_area,
K_THREAD_STACK_SIZEOF(my_stack_area), WORKQ_PRIORITY,
NULL);
strcpy(my_work.name, "Thread0 emulate_work()");
k_work_init(&my_work.work, offload_function);C9. 不再在 while 循环里直接执行 emulate_work,而是调用 将工作项提交到工作队列。k_work_submit_to_queue()
k_work_submit_to_queue(&offload_work_q, &my_work.work);C现在 thread0 把 emulate_work() 的处理任务下放给了优先级更低的工作线程。这意味着高优先级上下文里要做的事情变少,之后它就进入 20 毫秒休眠。对应的效果是:thread1 可以获得更多执行时间,来自 thread0 的抢占干扰变少。
10. 构建应用程序并将其烧录到您的开发套件中。使用串口终端,您应该看到以下输出:
*** Booting nRF Connect SDK ***
*** Using Zephyr OS ***
thread0 yielding this round in 0 ms
thread0 yielding this round in 0 ms
thread1 yielding this round in 26 ms
thread0 yielding this round in 0 ms
thread0 yielding this round in 0 ms
thread1 yielding this round in 26 ms
thread0 yielding this round in 0 ms
thread0 yielding this round in 0 ms
thread1 yielding this round in 27 ms
thread0 yielding this round in 0 ms
thread0 yielding this round in 0 ms
thread1 yielding this round in 27 ms
thread0 yielding this round in 0 ms
thread0 yielding this round in 0 msTerminal上述日志是在运行频率为 128 MHz 的 nRF54L15 DK 上运行的。如果在 64 MHz SoC 上运行,所需时间应为上述日志中显示的两倍。
线程运行时序大致如下

如您所见,thread0 现在一轮处理在不到 1 毫秒内就完成,随后进入休眠,给其他更低优先级线程留出更多运行时间。对于 thread0 来说这是可行的,因为 emulate_work() 的执行延迟是可以接受的。 同时可以观察到,对比没有使用工作队列做任务下放的场景,thread1 完成一轮任务的耗时明显缩短。 这就是良好架构设计的范例:仅把紧急任务放在高优先级线程执行,非紧急任务下放至合适的低优先级线程。 作为 RTOS 应用开发者,你应当熟悉内核提供的各类服务并合理运用,以此避免不必要的延迟。
此练习的解决方案可以在 GitHub 仓库 l7/l7_e3_sol 中找到。