new task after vTaskStartScheduler

mcirsta wrote on Wednesday, April 28, 2010:

I’m looking to dynamically create and delete tasks after vTaskStartScheduler was called ( a sort of thread pool ).  

I’ve tried using a function pointer for a task defined before calling vTaskStartScheduler and then modifying the pointer at some point but that didn’t work , it still ran the old function defined when the task was created.

I’ve also tried deleting a running task which worked but then creating a new one with xTaskCreate failed. I’m thinking that xTaskCreate won’t work after  vTaskStartScheduler was called.

Is there any way to add new tasks after  vTaskStartScheduler was called ?

Thank you.

rtel wrote on Wednesday, April 28, 2010:

There should be no reason why you cannot create and delete tasks after the scheduler has been started using the normal API functions.  Creating a task will fail if there is insufficient heap space for the task stack.  You can use xPortGetFreeHeapSize() to check how much stack is free.

To delete tasks you must ensure the memory the kernel allocated to the task can be freed.  This means that the idle task must not be starved of processing time, and you cannot use heap_1.c.

Regards.

mcirsta wrote on Thursday, April 29, 2010:

Thanks for your answer , I’ll investigate this problem further.

mcirsta wrote on Thursday, April 29, 2010:

I’ve checked and it seems the problem was indeed that there was not enough memory available for starting another task.  Also deleting the task didn’t free up any memory because as you said the memory allocation was implemented using heap_1.c

Isn’t it possible in any way to divert a task to do something else without deleting it and creating another one. I’m guessing using a function pointer instead of a function name when starting a task won’t work ?

richard_damon wrote on Thursday, April 29, 2010:

Once a task has started, trying to do something outside that task to redirect it elsewhere is a very bad idea, as you have practically no idea where the task currently is.

Note that when you give the create task call a function name, that name IS passed to the create function as a function pointer. Changing it afterward won’t change what the task is doing, as that value is just used as where to start.

If you want a task to do different things, then what you need to do is have the task in part of its loop query what it is being asked to do and then go off and do that task, and when it checks again later maybe it does something else.

The big question comes, why do do want to change on the fly what a task does?

mcirsta wrote on Friday, April 30, 2010:

Yes , you are right , the best way would be to check for something in the task loop.

As to why I would want such a thing , it’s because I want to implement a sort of thread pool concept. There are 2 fixed tasks that do the same thing and 2 tasks that can be assigned to various other tasks and then when that task is finished be used for something else.

Thank you all for your support , it helped .