Goroutines in Standard ML

A goroutine is a lightweight thread of execution.

fun f from =
    let
        fun loop 0 = ()
          | loop i = (print (from ^ " : " ^ Int.toString(i) ^ "\n"); loop (i - 1))
    in
        loop 2
    end

fun main () =
    (
        f "direct";
        val _ = spawn(f "goroutine");
        val _ = spawn(fn () => (print "going\n"))
    )
    
(* The spawn primitive could be considered similar to launching a new concurrent thread in other high-level languages like Go or Haskell. *)

val _ = main ()

Suppose we have a function call f(s). Here’s how we’d call that in the usual way, running it synchronously.

f "direct"

To invoke this function in a concurrent thread, we can use a spawn function (assuming it exists as part of an SML concurrency library). This new concurrent thread will execute concurrently with the calling one.

spawn(f "goroutine")

You can also start a concurrent thread for an anonymous function call.

spawn(fn () => (print "going\n"))

Our two function calls are running asynchronously in separate threads now. Normally, we would wait for them to finish using some thread synchronization mechanism, but for simplicity, this example just prints “done” after waiting.

Next, we could move on to learning about other concurrency features in SML, such as channels (if provided by the implementation).