Skip to content

Instantly share code, notes, and snippets.

@Fristi
Created April 1, 2013 12:57
Show Gist options
  • Save Fristi/5284806 to your computer and use it in GitHub Desktop.
Save Fristi/5284806 to your computer and use it in GitHub Desktop.
Simple example of Stm in F#
let spawn (f:(unit -> unit)) = let t = new Thread(f) in t.Start(); t
let valueStm = newTVar 0
let incStm() = stm {
let! current = readTVar valueStm
let newValue = current + 1
do! writeTVar valueStm newValue
}
let funcStm() = for _ in 1..10000 do incStm() |> atomically |> ignore
seq { for _ in 1..100 do yield spawn funcStm }
|> Seq.iter(fun t -> t.Join())
let result = stm {
let! v = readTVar valueStm
return v
}
printfn "Result is: %d" (result |> atomically)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment