View unanswered posts | View active topics It is currently Wed Jul 23, 2014 4:14 am






Reply to topic  [ 4 posts ] 
Tasks and Multi-Threading 
Author Message
Rookie

Joined: Wed Jan 15, 2014 8:25 pm
Posts: 2
Post 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.


Thu Jan 16, 2014 10:25 am
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 556
Post 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!

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Tue Jan 21, 2014 11:44 am
Profile
Rookie

Joined: Wed Jan 15, 2014 8:25 pm
Posts: 2
Post 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.


Wed Jan 22, 2014 11:12 pm
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 601
Post 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);
   }
}

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Fri Jan 24, 2014 12:28 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.