Return Statement in task

savindra wrote on Wednesday, May 18, 2016:

Hello ,

I want to use soemthing like this in a task:

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.

westmorelandeng wrote on Wednesday, 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.

rtel wrote on Wednesday, 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] );
  1. 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 );


westmorelandeng wrote on Wednesday, May 18, 2016:

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

savindra wrote on Wednesday, 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


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



rtel wrote on Wednesday, May 18, 2016:

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

richard_damon wrote on Wednesday, 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.

savindra wrote on Thursday, May 19, 2016:

It seems to be a great idea.

Thanks Richard.