I am attempting to use an array of TaskHandle_t pointers for ease of use for a project. It is used with vTaskCreate, vTaskSuspend, vTaskDelete and vTaskResume. So far, it appears to work will with one single task, but not multiple. The point is that there are several similar threads working with the same function, but there are slightly different parameters needed for slightly different behavior. For ease of use, I though it would be easiest to distinguish between them by using an array of task handler pointers to define behavior. However, although everything compiles now, I am getting some runtime errors and I want to be sure this technique is possible to use. Many thanks in advance!
There should be no problem with using an array of pointers to store the values. You do need to make certain that you are accessing that array properly.
One instance of Richard’s term “properly” relates to race conditions which may arise from tasks attempting to access tasks that have been deleted.
As has been reiterated many many times here, deleting and creating tasks dynamically at runtime is a poor design and should be avoided. There are very very few use cases which would justify such a system design.
I am now curious about the full extent of what “properly” means for FreeRTOS tasks and how calling vTaskDelete can be avoided.
All tasks are normally created as infinite loops that ideally spend a lot of their time dormant, waiting for events, so instead of creating a task at runtime, you normally create it at startup and suspend it until a trigger puts it back into action.
Deleting a task bears many many pitfalls, so there really isn’t a point in putting an infra structure around it. If you are unsure how to render your system requirements into a task architecture, sketch it out here, and I am sure many will be able to draft an architecture that gets away without dynamic task deletion and recreation.
Please query the forum for xTaskCreate and vTaskDelete, this issue has been discussed so many times that really there is nothing more to add.
To “Properly” delete a task, you need to know that task is “dormant” and not claiming any resource that would need to be cleaned up. This is sort of hard to do, unless the task signals another task to say “Delete Me” and then just suspends itself or blocks forever. Simpler in that case normally is to have the task just delete itself (Its does require time to get to the Idle task to finish the task cleanup).
Unless you really need to dynamically allocate the task resources (primarily the stack memory) between different uses, it is best to just have the task block until it is needed again.
Forgive my ignorance, I’m still a student doing very simple ‘test case’ programs, but, what resources would need to be cleaned up? Do you mean pointers and ownership, or is there something else specific to tasks that I should know? Is there a way to get a task to delete itself from a polled event at runtime? Just curious, many thanks! And thanks for the info!
This is any resource that the task might be using. In other words, the task needs to come to a safe-to-delete state and then ask some other task to delete it and suspend itself. At that point, it is better that the task deletes itself.
Yes, by calling
vTaskDelete( NULL );, a task can delete itself.
What problem are you trying to solve by dynamically creating and deleting tasks?