Concurrency

Threads, shared memory, and the synchronisation primitives.

vlatest

PickleScript concurrency is built on OS threads and explicit synchronisation primitives, in std.thread.

std.thread.spawn starts an OS thread running a zero-argument function and returns a Task you can join. Synchronisation is explicit: Mutex, CondVar, Semaphore, AtomicInt and Channel.

The reliable shape is a named top-level function spawned through a lambda, with a channel carrying the result back:

import { Channel, spawn, sleep } from std.thread

fn producer(ch: Channel) {
    for (i in 1..4) {
        ch.send(i)
    }
    ch.close()
}

fn main() {
    let ch = Channel(8)
    spawn(fn() { producer(ch) })

    var done = false
    while (!done) {
        match (ch.tryRecv()) {
            case some(v) -> { println("received {v}") }
            case none -> {
                if (ch.closed()) {
                    done = true
                } else {
                    sleep(1)
                }
            }
        }
    }
    println("drained")
}

The full API, covering channels, condition variables, atomics and the exact signature of each call, is in Threads.

Two things that will bite you

A lambda cannot mutate a binding it captured. A binding is captured by value and the body is hoisted, so the enclosing binding is out of reach. Put the mutable state in a class instance and assign to its field instead.

Polling without yielding deadlocks. A while loop that spins on ch.tryRecv() and does nothing in the none branch will starve the producer thread and hang the program. The sleep(1) in the example above is load bearing, tryRecv is a non-blocking poll, and something has to give the other thread the CPU.

The synchronisation types

Mutex has a no-argument constructor and two methods, lock() and unlock(). There is no with(...) helper and no re-entrancy:

Type Construction Methods
Mutex Mutex() lock(), unlock()
CondVar CondVar() see Threads
Semaphore Semaphore(permits) acquire(n), release(n)
Channel Channel(capacity) send, tryRecv, close, closed

Channels carry int

Channel is typed int in both directions. A string or a List never reaches the runtime, so box the value yourself if you need more than an integer.

The shape of the problem

  • Threads share one address space. Lists, maps and class instances are references, so two threads can observe the same object.
  • let bindings are immutable and var bindings are per-thread; neither makes a shared object safe to mutate. That is what the mutex is for.
  • join blocks. There is no structured concurrency and no cancellation a task runs until its function returns.
  • A task's result is not visible to the spawning thread until you join it. Reading a shared field straight after spawn will usually show the value from before the task ran.