Spawn

From APL Wiki
Revision as of 10:46, 11 September 2022 by Adám Brudzewsky (talk | contribs) (Text replacement - "<source" to "<syntaxhighlight")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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.

Usage

Spawn is used to run background tasks, for example for monitoring or handling incoming requests. Since the thread number is returned, ⎕TSYNC ("Thread Synchronise") is provided to retrieve the result, given the thread number. If the result is ready before ⎕TSYNC is used, it prints to the session if applicable.

Spawn is often used in conjunction with the Each operator (¨) to launch multiple threads in parallel.

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: {nilFun}&⍬

Examples

      ÷&4         ⍝ Reciprocal in background
0.25
      ⎕←÷&4       ⍝ Show thread number; result prints afterwards
1
0.25
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 (⎕DL) 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.

      t←⎕AI[3] ⋄ ⎕DL¨⍳3 ⋄ ⎕AI[3]-t
6138
      t←⎕AI[3] ⋄ ⎕DL&¨⍳3 ⋄ ⎕AI[3]-t
0
      t←⎕AI[3] ⋄ ⎕TSYNC ⎕DL&¨⍳3 ⋄ ⎕AI[3]-t
3046
Works in: Dyalog APL

External links

Lessons

Documentation


APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare RootRound
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axisIdentity (Null, Ident)
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)Identity (Lev, Dex)
Quad names Index originComparison toleranceMigration levelAtomic vector