Delete Tasks

malacay wrote on Thursday, December 01, 2011:

Hi,

I want to build a state machine.
Task 1 and Task 2 start and will be processed.
Once these have been processed Task3 should be executed.
Then everything will repeat itself.
problem:
The tasks are executed only once.

//global Variable
state=0;

main()
{
xTaskCreate( vZustandmanager, “Zustandmanager”, configMINIMAL_STACK_SIZE, NULL, 1, NULL );
vTaskStartScheduler();
}
void vZustandmanager( void *pvParameters )
{
 
  while(1)
  {
    switch (state)
    {
    case 0:
      xTaskCreate( vTask10, “Task 10”, configMINIMAL_STACK_SIZE, NULL, 2, &xTask10Handle );          
      state=1;
      break;
    case 1:
      xTaskCreate( vTask12, “Task 12”, configMINIMAL_STACK_SIZE, NULL, 2, &xTask12Handle );
      state=0;
      break;
       }
  }
}
void vTask10( void *pvParameters )
{
 
      timer1();//init
      timer1Interrupt = 0;        //volatile
      set_timer1(3417); //250ms
      DVK_setLEDs(0x2000);
      while(timer1Interrupt == 0);//OF Bit Interrupt
      DVK_setLEDs(0x0000);
  taskdelete++; //global variable
  vTaskDelete( &xTask10Handle );
}
void vTask12( void *pvParameters )
{
   DVK_setLEDs(0x0200);

   vTaskDelete( xTask12Handle );
}

oek wrote on Friday, December 02, 2011:

I suppose one of these lines is incorrect:

vTaskDelete( &xTask10Handle );
vTaskDelete( xTask12Handle );

Owen

richard_damon wrote on Friday, December 02, 2011:

First a comment on design, do NOT just wildly create and delete tasks, this is almost always a wrong solution.

One big problem I see with your code is your Zustandmanager task continually runs creating tasks. It may get held for a moment as the tasks that it creates run, but it will never give up the processor for the idle task, and that means that the tasks that are deleted are never cleaned up from, so you WILL run out of memory.

Second, if your goal is for tasks 1 & 2 to run, and when they finish for task 3 to run, and when it finishes tasks 1&2 to run again and so on, then none of these tasks should be deleted, but should just wait on semaphores to tell them when to go, and then as tasks finish they set the appropriate semaphore to tell the next task to go.