 Let's have a look at what is happening when the task would like to finish its job earlier and usage of function task yield Let's analyze the situation where the task is finishing its job before the time elapsed given by the time but by the cystic What it can do? It can go to the blocked state using already mentioned as delay function but in this case we need to precisely define the delay we would like to specify and In many cases it is not known We just would like to be active in the next time slot dedicated to this task and not to go to the block state for given time So this is not the best choice The good choice would be to move our active task somehow From running mode to the ready state and for this we have a dedicated function. It is called OS treat yield Let's have a closer look how it works OS treat yield function it is used to end the task activities once the job is done and We don't want to wait for tick and nor we don't want to make the next iteration of our task body function and The effect of this function is to move the active task from run mode to ready mode And trigger the pent as V interrupt to switch the context to the next task from ready list It makes sense if you have few tasks on the same priority level Otherwise the yielded task will be executed again as the task with the highest priority So let's analyze the situation like on below picture We've got two tasks task two and task one on the same priority task one is Let's say executing its code and it's finishing before the tick time So it is calling OS treat yield I street yield is a triggering pent as V software interrupt to switch the context from task one to Another one from the ready list. So in this case task two task two starts its execution And please have a look that it's interrupted by the cystic when the tick time elapsed and then Cystic is calling again pent as V and selecting the next task from the ready list Which is again task one. So there is one disadvantage of this yield function Disadvantage is that the next task which would be selected by the scheduler will have to its disposal Only the remaining part of the tick time, which is not always the desired amount of time So please remember about this. This is really important Maybe not limitation, but argument to think over whether it is good to call the yield function from the active task Let's have a closer look on OS treat yield API function step by step So I street the yield function It is coming from CMC's OS V2 and is calling task yield function from 3R to S API This task yield function is calling port yield, which is defined in port macro dot h file And this function triggers pent as V interrupt to request a context switch to the next task from the ready list Below we can see the example It is a version for IRC compiler at the beginning We are setting the bit pent as V just to trigger the pent as V and we are using two Instruction barriers to be sure that the complete setting of the bit will be done before the execution of the next instruction So this is how it is done step by step It is possible as well to use this function from the interrupt But in this case instead of port yield there would be an execution of port yield from ISR But the effect would be exactly the same Let's test the yield function in practice. So please come back to the previous exercise Let's start from the Cubemix perspective or STM32 Cubemix application Please select the 3R to S configuration button and Please go to the tasks and queues Within task and queues, please unify the priority level for both of the tasks task 1 and task 2 It can be OS priority normal value Then please generate the code and come back to main.c file Within main.c file, we will modify the body functions for both tasks within task 1 We will remove everything within the while for for endless loop. We will keep only the task underscore action 1 and We will add OS treat yield function after it Within start task 2 function, we will keep within the infinite loop only task action with argument 2 Then let's compile the code and start a debug session As a result, we should see the following situation At the beginning task 1 would be executed. So we can see the task action 1 And then it will go immediately to the ready state giving the space for the remaining time When it's assigned 1 millisecond for task 2 Assuming that our MCU is really fast We can expect that within this 1 millisecond more than once we can execute the task action In such a case we can see on the screen that there would be one then a few times would be two Because task 2 would be executed continuously Its endless loop would be executed continuously till the end of the 1 millisecond time And then after it we will see again one because there would be a cystic Which would call the switch of the context from task 2 to task 1 So we could see that there would be much more execution much more time Dedicated for task 2 then task 1 because task 1 would resign from its assigned time to task 2 As it doesn't need to spend complete 1 millisecond for its operation But of course it depends on the code which is located within the endless loop And in case there is much more coding within this loop, the final effect could be different Please check it on your side So at the beginning we can see that task 2 is executing many many many times And then from time to time we can see task 1 The explanation for this is the following Within our code we have specified the time slice for each task for 1 millisecond And within this 1 millisecond the task is executing the code from this endless loop So in case of task 2 it will be sending these two over ITM interface one by one As fast as possible within this 1 millisecond This is why we can see a lot of let's say sign of life from task 2 And as task 1 is sending only once its data And then it is going immediately to ready state We can see from time to time only the sign of life from task 1 So the effect is visible but please remember that yield function should be used Among the tasks with the same priority So if you would like to block somehow or to finish earlier Let's say that the job of the task with higher priority Better use OSD line instead Which would put higher priority task to the blocked state Thank you for watching this video