Quality RTOS & Embedded Software

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


Return Statement in task

Posted by savindra on May 18, 2016

Hello ,

I want to use soemthing like this in a task:

if(true) { return; } I know it is not allowed to use such type of instruction in the task.

Is there alternate way to do this in FreeRTOS? What i want to do is stop the task when a condition is true and start normally at next cycle.

Return Statement in task

Posted by westmorelandeng on May 18, 2016


You can use vTaskSuspend(NULL); inside the task and then vTaskResume(&Handle); - I think that syntax is correct. You can use semaphores too.

Take a look at some of the example code that does this.

If you return from a task your task stack goes bye-bye. Try it - it's a learning experience.

This made me laugh because I am working on a port and it had a ton of returns - I converted a lot of code to tasks - and it died - and I was certain I had found all of the returns - but I missed some and it sure as heck crashed the port - so be careful - each task has to run in its own context.

Good Luck! John W.

Return Statement in task

Posted by rtel on May 18, 2016

Do you mean you want to restart a task? If so, there is no built in way of doing that, but you could:

1) Implement a function as follows:

void RecreateTask( void * pvParameter1, uint32_t ulParameter2 )
     /* Delete the original task, the handle to which is passed
     in using the first parameter. */
     vTaskDelete( ( TaskHandle_t ) pvParameter1 );

     /* Recreate the task. */
     xTaskCreate( [whatever] );

2) When you want to exit the task, use the xTimerPendFunctionCall() API function to send the above implemented function to the Timer task, where it will be executed.

xTimerPendFunctionCall( RecreateTask,
                         0, /* not used. */
                         portMAX_DELAY );

/* If the timer task has a high enough priority the following code
will not execute because the timer task will have deleted this task.
If the timer task has a lower priority then suspend this task so it
allows the timer task to execute and delete this task. */
vTaskSuspend( NULL );


Return Statement in task

Posted by westmorelandeng on May 18, 2016

I didn't see the reply before I posted - maybe I misunderstood the question. Recreating the task is a nice feature.

Return Statement in task

Posted by savindra on May 18, 2016

I was also thinking to recreate the task. but i have one doubt : My task is running at 10ms interval, if i recreate it will not come directly into while loop. I mean it will do the intialisation also if i recreate the task.

Don't you think it will loose the deterministic behaviour.

void vMy_Task( void *pvParameters ) { ///intialisation code

while(1) {

	vTaskDelayUntil( &xNextWakeTime, configMODBUS_MASTER_FREQUENCY_10MS );
   ///do this work



Return Statement in task

Posted by rtel on May 18, 2016

I think from your reply it is clear I don't understand what it is you want to do.

Return Statement in task

Posted by richard_damon on May 18, 2016

If you want to terminate the task to be created again fresh for another job, just have the task delete itself: vTaskDelete(0);

Normally, I don't keep deleting and creating tasks, as it is inefficient, but the task has out the outer level a while(1) loop, and at the top of that loop something that causes the task to block until it has something to do (a vTaskDelay, or wait on a semaphore/event flag, or get from a queue). If at some point in processing, I am done and want to wait for the next operation, you just use a continue statement.

If you want to use a return, just make that part of the code a function that the main loop of the task calls, then THAT function can return and the main loop goes up to wait again.

Return Statement in task

Posted by savindra on May 19, 2016

It seems to be a great idea.

Thanks Richard.

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

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

Latest News

FreeRTOS v10.2.1 is available for immediate download. MIT licensed, includes 64-bit RISC-V, NXP Cortex-M33 demo & Nuvoton Cortex-M23 demo.

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

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

Cadence Tensilica Cortes

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