How to restart the task

I want to restart a task, but I do not find any of direct way to do it. Currently, I delete the task and re-create it again to restart the task. It will release the memory of stack, and allocate another memory space as stack. If I restart the task several times, it has a risky to cause memory fragmentation. How should I do to restart a task?

How to restart the task

If (you are using heap_2) && (the stack size allocated to the task is always the same) && (you are not dynamically creating and deleting any other objects (queues for example)) then you should not suffer from fragmentation as the same memory block will just get reused. This is easy enough to test.

How to restart the task

Unfortunately, I use malloc function call (Newlib C library) to allocate memory instead of heap_2, and the other task may dynamically allocate memory when the task is restarted. I am not sure the impact of memory fragmentation on the more complicate application, so I need more safe mechanism to restart the task. Actually, I want to implement the plug-ins function in my system. It will stop the task of plug-ins, load the other code to the memory, and restart the task again to run the code of plug-ins. Maybe I can suspend the task and modify the content of TCB or whatever, and resume the task. But, dose exist any simple & portable way to do that?

How to restart the task

Why not just modify the code in the task so you can set a flag or send a message to it to cause it to restart.  e.g. boolean fMyTask_Restart; void MyTask (void *pvParameters) {   for (;;) {     fMyTask_Restart = FALSE;     /* Any other task initialization here */     while (!fMyTask_Restart) {       /* You main task code here */     }   } } Now any time you want to restart the task just set fMyTask_Restart to TRUE.  You can also check the flag to make sure the restart occurred. Chris