Global variables in bare metal can be executed one by one. If one of the two functions in while adds 1, then it adds 2. However,
in the operating system, global variables may be interrupted. During the process of increasing variables, When a is added to half of the task, it is cut out, and then task 2 is to add 1 to the variable a, and then switch to task 1. In task 1, add the variable that has not been added, and then the variable a is still 1. The addition of 1 to the intermediate task 1 is added through the intermediate amount, so after the two tasks are executed, the a variable is still 1.
Use the queue API function:
just like entering the critical section, others cannot interrupt me. In this area, the equivalent bare-metal program
Queues support mechanisms for sleeping and waking up.
In a bare-metal program, when executing tasks by judging variables, it will take up a lot of CPU.
But it's different in the queue. You can do things by reading the queue. When you can't read the data, it will fall into sleep.
How can it wake up? The answer is that when task A is executed multiple times, the condition is established and the queue starts to be written, then B can be woken up.
After waking up, B can read and execute the following things
So when multiple tasks are sleeping, how do I know who to wake up when I write to the queue?
The function in the queue is used here: Queue.list. For example, if the B function is dormant, it will insert itself into the queue. Then when A wakes up, it will look for it from that linked list.
Ring buffer: save data
Summary:
turn off interrupts, ring buffer, linked list