Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Replacing a task

Posted by hassanbd on June 24, 2014

Hi, 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.

Replacing a task

Posted by davedoors on 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 );


Replacing a task

Posted by hassanbd on 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); }

Replacing a task

Posted by rtel on 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 );

} ~~~~


Replacing a task

Posted by hassanbd on 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!

Replacing a task

Posted by rtel on 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.


Replacing a task

Posted by richard_damon on 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:

[code] int func = 1; void function taskA(void* parm) { for(;;){ switch(func){ case 1: func1(parm); break; case 2: func2(parm); break: case 3: func3(parm); break; default: // what do you want to do here, maybe delete ourselves? break; } } } [/code]

Replacing a task

Posted by hassanbd on 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

Replacing a task

Posted by hassanbd on June 25, 2014

Thanks. I will keep this in mind.

[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner