Hi. I have two tasks: Task1 and Task2. While Task2 is running, if event1 occurs, Task2 will be blocked and Task1 runs. Then, if event2 occurs, Task1 will be blocked and Task2 runs from the beginning.
I tried to delete task repeatedly but it leads to heap overflow. I searched for heap management and realized that I can use heap_4c for my projects. But I’m unable to find the examples using heap_4c for deleting task repeatedly.
I read all similar topics on the site but still couldn’t solve my problem. Hope you can help me. Thank you all.
I wouldn’t recommend to heavily create and delete tasks. That’s not efficient and might get complex e.g. related to manage the resources needed for the tasks or allocated by the tasks itself.
Common and robust practice is to create the necessary tasks and keep them running and synchronize their execution as needed by using an appropriate signaling mechanism like task notification, (binary) semaphore, message queue, etc.
I see. But using semaphore or message queue only blocks the task, and it will resume. But I want it to start from the beginning.
Then just code it accordingly. I mean execute the code you want after getting signaled and wait again…
No need to delete and re-create tasks all the time.
Avoid, if possible, task creation using the heap, e.g. use vTaskCreateStatic() model.
If not possible, then create the tasks at the very beginning in your program and never delete them.
Instead of deleting tasks use, as already said, syncronization mechanisms, like binary semaphores: FreeRTOS task notifications, fast Real Time Operating System (RTOS) event mechanism
And in order for your tasks to block indefinetely, for the parameter xTicksToWait
choose the symbolic constant portMAX_DELAY
.
“If INCLUDE_vTaskSuspend is set to ‘1’ then specifying the block time as portMAX_DELAY will cause the task to block indefinitely (without a timeout).”
(Comment taken from: This page describes the xSemaphoreTake() FreeRTOS API function which is part of the RTOS semaphore API source code function set.)
Do you even need two tasks? Sounds to me like two functions being called alternating depending on an external input(Event) from one task.