Message Passing: Example

Let’s discuss message passing with the help of an example.

We'll cover the following

Example

Let’s use what we have discussed so far in a simulation program.

The following program simulates independent robots moving around randomly in a two-dimensional space. The movement of each robot is handled by a separate thread that takes three pieces of information when started:

  • The number (id) of the robot: This information is sent back to the owner to identify the robot that the message is related to.

  • The origin: This is where the robot starts moving from.

  • The duration between each step: This information is used for determining when the robot’s next step will be.

That information can be stored in the following Job struct:

Get hands-on with 1200+ tech skills courses.