Created
April 1, 2013 12:57
-
-
Save Fristi/5284806 to your computer and use it in GitHub Desktop.
Simple example of Stm in F#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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