Replacing a task

hassanbd wrote on Tuesday, June 24, 2014:

I have an application where I want a task to replace itself with another task, so that the new task have the same handle. I have worked with SALVO for a while and there is API OS_Replace to do it. I was wondering if there is such an option in freeRTOS. If not, can someone suggest a neat way of doing it? Thanks in advance.

davedoors wrote on Tuesday, June 24, 2014:

You can use the same handle, although the value of the handle might change. For example

void aFunction(void){
TaskHandle_t aHandle;

// Create a task, saving the handle in aHandle
xTaskCreate( ...., &aHandle );


// Delete the task

// Create another task using the same handle variable
xTaskCreate( ...., &aHandle );


hassanbd wrote on Tuesday, June 24, 2014:

Thanks for the reply. But is there any way for a task to replace itself? What happens if I try this:

void TaskA (void *pvParam){

xTaskCreate (…, &aHandle); //aHandle refers to TaskA before this point
vTaskDelete (NULL);

rtel wrote on Tuesday, June 24, 2014:

I’m not 100% sure if I understand your question, but if I do I don’t think there is a way of doing exactly what you want. Maybe if you explained what you were trying to achieve (why you wanted to do it) then we may be able to suggest something.

You can of course have a task create a new task before deleting itself. For example:

void vTaskX( void *pvParameters )
    /* Task code goes here. */

    /* Have vTaskX create another instance of vTaskX (or any other task)
    before it deletes itself. */
    xTaskCreate( vTaskX, ....
    vTaskDelete( NULL );


hassanbd wrote on Tuesday, June 24, 2014:

The first way by Dave requires another task to handle the replacement. The second way requires different handles for the tasks.

My application has four mutually exclusive tasks- only one of them should be eligible at any point of time. So I wanted to have one handle for these four tasks. One of the tasks has a timeout behavior i.e. it waits for a certain time and then should give way to another of the four tasks. It would have been really neat if I could just call a replace function from the task itself, but it seems there isn’t a way.

Again, Thanks for the support!

rtel wrote on Tuesday, June 24, 2014:

Sorry, but I still don’t think I understand what it is you are trying to do.

You have four tasks, and it sounds like you only want one task to be running at a time. There are several ways that could be achieved (for example, suspending the tasks you don’t want to run, have the tasks wait on an event bit until signalled to run, assign the running task a higher priority, then lower its priority and raise the priority of the next task in the sequence to run, etc.) - but I don’t understand why you would only want one task handle, or what you mean by ‘replace’ - maybe because I am not familiar with SALVO (which if I recall correctly is not preemptive?).

Would it work for you to have 5 different handles - one for each of the four tasks (xTask1, xTask2, xTask3, xTask4 for example) and then one that points to the task you want to run. So say you had another handle called xRunningTask - when the task referenced by xTask1 was the running task you just set xRunningTask to xTask1 (xRunningTask = xTask1) so xRunningTask will always point to whichever of the 4 is running at any particular time.


richard_damon wrote on Tuesday, June 24, 2014:

One way to do this, IF you have operations task1(), task2(), task3() etc, that want to be run in different conditions, create a supervisory functions taskA(), that is the routine actually created as the task, and have it call the desired operation.

Something like:

int func = 1;
void function taskA(void* parm) {
        case 1:
        case 2:
        case 3:
// what do you want to do here, maybe delete ourselves?

hassanbd wrote on Wednesday, June 25, 2014:

Its really not a problem of implementation. I just wanted to be neat. Having one handle for 4 mutually exclusive tasks prevents even the programmer from running more than 1 task at any time by mistake. Maybe I am being too obsessive…

The 5 handle solution is nice. I think I may try it out. Thanks

hassanbd wrote on Wednesday, June 25, 2014:

Thanks. I will keep this in mind.