Workers
The same heavy sum is run twice: once on the main thread and once in a Worker. A counter next to it updates every 60ms, which shows which run stops the screen.
Measurement
JavaScript has one thread. A synchronous computation stops the screen regardless of how it is written. This is unrelated to the actor mechanism.
| Where the 300ms computation runs | Longest pause seen by another actor |
|---|---|
| Main thread | 338 ms |
| Worker | 34 ms (same as idle) |
Specifying the thread
An actor running in a Worker is referenced with an ordinary ActorRef.
a := Worker spawn: (Adder named: '集計' to: sink) on: w.
(a ask sumTo: 100000) forced printNl.
The only line that names the thread is Worker spawn:on:. ask and tell are written the same as for an actor on the same thread. An ActorRef does not record which thread the actor runs on.
Values that cannot cross threads
State and messages are copied when they cross a thread. A block refers to variables in the scope where it was created, so it cannot be copied. Placing a state that contains a block on a Worker is a compile error that names the offending field.
the state placed on a worker cannot cross an actor boundary: it is Job,
whose work is Block[Int -> Int], and a block closes over the scope it was
made in
Decimal, Date, and Dict cross with their types intact. The browser's structuredClone loses class information, so the LPScript runtime has its own wire format for values passed between threads.
Errors
When an actor in a Worker stops with an error, the monitoring actor receives down:, as it would on the same thread.