Launching parallel tasks

I. Call to a turtle's member function. It is done with a natural, object-oriented syntax. Call to the member function in the POOL language creates a task that is placed in the task queue of the called turtle. This task is executed in parallel to the caller's code. If the function outputs a result, it is passed back to the caller. Execution of the caller's code is suspended on the first actual use of the result, if this result is not ready yet (reading the result of a function).
Typpical situations:
    - turtle that should execute the function is known by its name or is available in a variable;
    - function is to be triggered on a single turtle for each call (then arguments can be different for each call);
    - function returns the result which is used by the caller.

Example 1:
Code draws circles with the draw_circle function called in the loop on a single turtle (version a), or called on many turtles that are doing the job simultaneously (version b).
The delay in the inner loop of the function allows to see the difference between code versions.

to draw_circle :h :dx [:dt 5]
  seth :h
  repeat 5 [
    repeat 180 [
      fd :dx rt 2
      sync :dt ;delay
    ]
    "dx *= 0.8
  ]
end

"n := 7

;a) single turtle:
;for [i 1 :n] [draw_circle 360 * :i / :n 3]

;b) parallel turtles:
"ts := (newturtles :n)
ht
foreach "t :ts [(draw_circle 360 * repcount / :n 3) @ :t]

Output: a drawing in the graphics window.

II. Signal hangling. Signals in the POOL language can be used to synchronize tasks of turtles. If a turtle has an active handling function for a signal when that signal is raised then the task is created and placed in the turtle's task queue. This task is executed concurently with tasks of other turtles.
Typical situations:
    - function is to be triggered on many turtles simultaneously;
    - caller does not have any access to the turtles that execute the function;
    - result of the function is not used by the caller.

Example 2:
Function draw_circle from the example 1 is replaced with the signal handling function onsignalgo.

to parallel :h :dx [:dt 5]
  to onsignalgo
    repeat 5 [
      repeat 180 [fd :dx rt 2 sync :dt]
      "dx *= 0.8
    ]
  end
  seth :h
end

"n := 7
repeat :n ["t := (newt $parallel 360 * repcount / :n 3)]
signal "go
ht

Output: a drawing like in the example 1.

III. Event handling. An event, just like a signal, causes creation of tasks for all the turtles that are involved in the event and have the appropriate handling function enabled.
Typical situations:
    - function is to be triggered by an event, its source can be another turtle or GUI element;
    - function is triggered on many turtles simultaneously;
    - function does not output any result.

Example 3:
Function draw_circle from the example 1 is now replaced with the event handling function: onmouseclick. Click on a turtle to trigger the drawing.

to parallel :h :dx [:dt 5]
  to onmouseclick :mousepos
    if distance :mousepos < radius [
      repeat 5 [
        repeat 180 [fd :dx rt 2 sync :dt]
        "dx *= 0.8
      ]
    ]
  end
  seth :h
end

"n := 7
repeat :n ["t := (newt $parallel 360 * repcount / :n 3)]
ht

Output: a drawing like in the example 1.

See also:

Event and signal handling

@ (access to class members)

Table of Content