ROBOTC.net forums
http://robotc.net/forums/

Tasks and Multi-Threading
http://robotc.net/forums/viewtopic.php?f=11&t=7699
Page 1 of 1

Author:  Deemeehaa [ Thu Jan 16, 2014 10:25 am ]
Post subject:  Tasks and Multi-Threading

Hi. My team and I are new to programming with C. We are using the Vex Cortex. We recently learned about multi-threading with tasks. Now for my question. Lets say that a task named "moveFoward" has been executed. What happens if the task "moveFoward" is called again before the original "moveFoward" finishes running. Will a new instance of the task be executed? Will the original task start from the top? Or will nothing happen? Thanks for your help.

Author:  JohnWatson [ Tue Jan 21, 2014 11:44 am ]
Post subject:  Re: Tasks and Multi-Threading

Calling a task from within that same task will cause the task to 'restart' itself; they aren't recursive.

Functions, on the other hand, are recursive and will create a new copy of that function on the call stack every time the function is called. Hope this helps, let us know if you have any other questions!

Author:  Deemeehaa [ Wed Jan 22, 2014 11:12 pm ]
Post subject:  Re: Tasks and Multi-Threading

Thanks for the clear explanation. Correct me if I'm wrong. If a task named "moveFoward" is running and "moveFoward" is called "moveFoward" will stop what it is doing and start from the beginning.

Author:  tfriez [ Fri Jan 24, 2014 12:28 pm ]
Post subject:  Re: Tasks and Multi-Threading

Yes, you can call startTask from inside of the same task running. It will simply jump to the top of that task and restart it from the first line of code inside of it. It will not "stop" anything, however, in the concept of stopping motors (i.e. the task started a motor, was then "restarted" via the startTask command - the motor will keep running until something tells it to stop, or all tasks complete)

Here's an example program of this in action. The variable "globalCounter" will never increment higher than 1.

Code:
int globalCounter = 0;

task counter
{
   globalCounter = 0;   
   while(true)
   {
      globalCounter++;
      wait1Msec(1);
      startTask(counter);
   }

}

task main()
{
   startTask(counter);
   while(true)
   {
      wait1Msec(1);
   }
}

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/