So far we have created programs that execute actions one after another. But sometimes we need to perform two actions at the same time. How do we achieve that?
- 22 Mar 2019
For example, if we want to program our robot to move along a given field and meanwhile it needs to move a hand with another motor(s). To be more precise let us say that we want our robot to move along a square and move a medium motor 240 degrees forward, then 240 degrees backward.
Each of the above tasks is relatively easy. The following program will make the robot to move in a square:
and the following program will make its hand move back and forth as described:
The problem arises when we want to perform the above two programs simultaneously.
In such cases, we need multiple threads. When computers have some actions to be performed they order them in threads. Each thread is executed by executing each of the tasks within one by one. If you need to perform two groups of actions simultaneously, you just need a new thread, so that each group will run in its own thread.
Courses and lessons with this Tutorial
This Tutorial is used in the following courses and lessons