 Let's have a look how to create and delete the tasks from running operating system-based application. It is possible to create a task and delete it from other task entry function. We'll create a simple example where within start task 1 we will create a task 2 of the same priority as task 1 and then within ready created task 2, so within start task 2 function, we will delete ourselves and give the space to task 1. And then in the next iteration task 1 we'll create again task 2 and the story will continue like this. It is possible to create a new one and delete a task from other task entry function. Let's start with a task creation. We can use an example from previous part, so for example yield test. And we can change the priority of task 1 to OS priority normal to have the same priority for both tasks. Then we need to comment task 2 creation part in main.cfile. Then within start task 1 function we can create task 2. As you can see task 2 will be created each one second, so we need to delete it fast enough to not stack our application. It will be the role of start task 2 entry function. Within this endless loop please use OS thread terminate function with a handler to the task to be deleted, task 2 in our case. Here in the picture you can see the code flow for our example. And what we can see, we can see that task 1 is executed first, then task 2, then again task 1 and task 2. Please have a look that we are not executing, we cannot see an effect of this line. This is due to the fact that after this the task is already deleted and this code is not executed anymore. Let's have a closer look on the OS thread terminate API function. So the function which is used to delete the task from our let's say task list. It is not a let's say the must to have function within the freeR2s. This is why we need to enable it within the configuration file of the freeR2s. To do this we need to include vtaskDeleteRow and this vtaskDelete is a name of the function within freeR2s API of this operation. So in fact the function we are just referring to, so OS thread terminate coming from CMC's OS v2 is calling vtaskDelete. It is done within CMC's underscore OS c file and the only argument both functions are using is the ID of the handler of the task to be deleted and how this vtaskDelete is working. vtaskDelete is defined within task.c file within freeR2s sources and as let's say deleting or removing the task is quite important part of functionality of the operating system it needs to be done again within the critical section. So at the beginning of this function call we are entering into the critical section using the macro task enter critical which is blocking all of the interrupts and operating system. So everything not related to the operating system will be still functional but all the interrupts which can execute the code of the operating system and the interrupts which are responsible for the context switch would be blocked. So we are operating on the base pre-mask register again. Okay so we are in the critical section then what is done? Scheduler is removing the task from the ready list using function uxlistremove and it's using the task from the waiting for the event task list. So for example removing from the list of the tasks which are waiting for some semaphore some queues. Then what is important that in case the task is deleting itself the function is switching execution to the next task calling function port yield within API. So it is important just to perform the switching of the context and important point which you can see in this green box is that even we will delete the task the memory is not automatically released so it is not coming back as a free resource to be allocated. It is done within the next execution of the idle task. The release of the memory from the task let's say or from the other components is in hands of the idle task it is one of its role during its execution. So this is an important message that it's not fully automatic process and the from the other side we really need some time to execute the idle task to perform this cleanup of the operating system. Thank you for watching this video.