How does a single-chip microcomputer realize real multi-threading?

The so-called multi-threading is simulated, and it is essentially single-threaded, because the CPU can only execute a piece of code at the same time.

The simulated multithreading is the rapid switching between tasks, which looks like they are executed at the same time.

It is said that the recent multi-core microcontroller, but the cost should be much higher.

For simulated multithreading, I know of two ways:

1. Polling system based on time slice

I have written such a system myself, the principle is to use systick to provide a time reference for the system, generally I use 10ms.

Then divide the overall product function into different tasks (threads), and assign a time slice to each task.

The scheduling time of each task marked in the red box, 1 represents 10ms, 50 represents 500ms, the execution frequency of each task can be different, so that it is convenient to release cpu resources for more needed tasks.

In the loop of the main function, the status of each task is always judged.

Here I encapsulate the task directly into a structure and call it through a function pointer to facilitate centralized management of different tasks.

If the task is ready, execute it, and wait for the task to be executed before the next task can be executed, so the essence is still polling.

It's just that each task can flexibly allocate scheduling time, less important tasks are executed, and important tasks are executed more often, which looks like the effect of multi-threading.

This method is better than the traditional while(1) to poll in terms of program architecture, and task management and scheduling are also much more flexible.

The disadvantage is that the task cannot be interrupted during execution, and it cannot be switched to other tasks immediately for execution.

I have recorded a tutorial for this system, and it is temporarily open for everyone to learn, and it may be deleted later.

If you want to refer to it, you can find the boundless single-chip microcomputer, or read the beginning of my article below .

" MCU Introduction to Advanced Opening Learning Path (with Tutorial + Tools) "

" MCU Introduction to Advanced Opening Learning Path (with Tutorial + Tools) "

" MCU Introduction to Advanced Opening Learning Path (with Tutorial + Tools) "

If it is a product with very demanding real-time requirements, this is not very suitable.

2.RTOS

RTOS should be the system closest to multi-threading of single-chip microcomputer, the purpose is to improve the real-time response ability of the system.

Let's say you are hosting a radio show and there is a weather segment, you want the weather to be broadcast every 10 minutes, but the radio program is 1 hour long.

If you use the while(1) polling method, you may encounter such a situation: the weather broadcast is occupied by its radio program, causing it to not proceed according to the scheduled time.

但是,如果你使用的是 RTOS,它会把天气播报分配到高优先级,并且它会保证每隔 10 分钟就运行一次天气播报程序。

播报完以后继续回到电台节目,这样即使有其他任务占用了大量的资源,也不会影响到天气播报程序的正常运行。

这就是 RTOS 的工作原理,它通过分配任务的优先级,保证任务按照预定的时间计划进行。

但本质也是模拟多线程,因为播报电台节目的时候,cpu资源被天气预报抢占了,它们两个任务并没有同时执行

单片机实现多线程技术是一个比较复杂的技术,不仅需要掌握单片机的相关知识,还需要掌握多线程技术的相关知识,比如任务调度、任务同步、任务资源分配、任务通信等等。

一般都是用现成的RTOS,自己手写一个难度太大了。

对于单片机多线程技术的学习,我不建议新手刚开始就去学RTOS。

应该多做一些基于单片机的项目,把编程基础打扎实。

到那个时候,再去学RTOS时间成本会低很多,理解也会更深刻。

Guess you like

Origin blog.csdn.net/weixin_43982452/article/details/129501663