Goroutines in Standard ML
A goroutine is a lightweight thread of execution.
Suppose we have a function call f(s)
. Here’s how we’d call that in the usual way, running it synchronously.
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.
You can also start a concurrent thread for an anonymous function call.
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).