Spawn: Difference between revisions

Jump to navigation Jump to search
1,344 bytes added ,  10:46, 11 September 2022
m
Text replacement - "<source" to "<syntaxhighlight"
(removed the dubble title i added)
m (Text replacement - "<source" to "<syntaxhighlight")
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{| class=vertical-navbox style="float:right; font-size:500%; margin:0 1ex;"
{{Built-in|Spawn|&}} is a [[monadic operator]] which applies its [[operand]] in a new [[wikipedia:green thread]]. Rather that returning the result of its operand, Spawn returns a [[shy]] thread number. This [[operator]] is specific to [[Dyalog APL]].
|<code>&</code>
|}
==== {R}←{X}f&Y ====


& is a monadic operator with an ambivalent derived function. & spawns a new thread in which f is applied to its argument Y (monadic case) or between its arguments X and Y (dyadic case). The shy result of this application is the number of the newly created thread.
== Usage ==


When function f terminates, its result (if any), the thread result, is returned.
Spawn is used to run background tasks, for example for monitoring or handling incoming requests. Since the thread number is returned, <syntaxhighlight lang=apl inline>⎕TSYNC</syntaxhighlight> ("Thread Synchronise") is provided to retrieve the result, given the thread number. If the result is ready before <syntaxhighlight lang=apl inline>⎕TSYNC</syntaxhighlight> is used, it prints to the [[session]] if applicable.


⎕TSYNC, used monadically, given the thread number can be used to wait for a result from that thread.
Spawn is often used in conjunction with the [[Each]] operator (<syntaxhighlight lang=apl inline>¨</syntaxhighlight>) to launch multiple threads in parallel.
If no ⎕TSYNC is in effect, the thread result is displayed in the session in the normal fashion.


 
As Spawn is an operator, it cannot be directly used with a [[niladic function]], since a niladic function is called immediately when its name is referenced. As a workaround, the niladic function can be wrapped in a [[dfn]] and called with a dummy argument: <syntaxhighlight lang=apl inline>{nilFun}&⍬</syntaxhighlight>
Note that & can be used in conjunction with the each operator ¨ to launch many threads in parallel.


== Examples ==
== Examples ==
<source lang=apl>
<syntaxhighlight lang=apl>
       ÷&4        ⍝ Reciprocal in background
       ÷&4        ⍝ Reciprocal in background
0.25
0.25
       ⎕←÷&4      ⍝ Show thread number
       ⎕←÷&4      ⍝ Show thread number; result prints afterwards
1
1
0.25
0.25
</syntaxhighlight>
      FOO&88      ⍝ Spawn monadic function.
{{Works in|[[Dyalog APL]]}}
The effect of running code in the background can be observed when a measurable amount of time elapses. In the first expression below, get the current time (in milliseconds) and then [[delay]] (<syntaxhighlight lang=apl inline>⎕DL</syntaxhighlight>) for each of 1, 2, and 3 seconds, eventually computing the elapsed time, which is slightly more than 1+2+3 seconds (6000 milliseconds). In the second expression, the delaying threads are launched, but we don't await their completion before computing the elapsed time. In the final expression, we synchronise the parallel running delays, giving us a total elapsed time only slightly longer than the longest delay.
       2 FOO&3     ⍝ dyadic
<syntaxhighlight lang=apl>
       t←⎕AI[3] ⋄ ⎕DL¨⍳3 ⋄ ⎕AI[3]-t
       {NIL}&0     ⍝ niladic
6138
       t←⎕AI[3] ⋄ ⎕DL&¨⍳3 ⋄ ⎕AI[3]-t
       &'NIL'    ⍝ ..
0
       t←⎕AI[3] ⋄ ⎕TSYNC ⎕DL&¨⍳3 ⋄ ⎕AI[3]-t
      X.GOO&99    ⍝ thread in remote space.
3046
</syntaxhighlight>
      ⍎&'⎕dl 2'  ⍝ Execute async expression.
{{Works in|[[Dyalog APL]]}}
== External links ==
      'NS'⍎&'FOO' ⍝ .. remote .. .. ..  
 
=== Lessons ===
      PRT&¨↓⎕nl 9 ⍝ PRT spaces in parallel.
 
</source>
* [https://chat.stackexchange.com/rooms/52405/conversation/lesson-26-concurrent-apl-threads APL Cultivation]
 
=== Documentation ===
 
* [https://help.dyalog.com/latest/#Language/Primitive%20Operators/Spawn.htm Dyalog]
 
{{APL built-ins}}[[Category:Primitive operators]]

Navigation menu