Created
November 3, 2015 22:21
-
-
Save owskio/b16568415e1b7995a289 to your computer and use it in GitHub Desktop.
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
//Modified from: http://fsharpforfunandprofit.com/posts/concurrency-actor-model | |
open System | |
let print str = printfn "%A" str | |
let slowWriter msg = | |
msg |> String.iter (fun ch-> | |
Threading.Thread.Sleep(1) | |
Console.Write ch | |
) | |
Console.WriteLine() | |
let makeTask log taskId = async { | |
for i in [1..3] do | |
log <| sprintf " [Task%i:Loop%i] " taskId i | |
} | |
print "==============" | |
[1..5] | |
|> List.map (fun i -> makeTask slowWriter i) | |
|> Async.Parallel | |
|> Async.RunSynchronously | |
|> ignore | |
print "==============" | |
let agent = MailboxProcessor.Start(fun inbox -> | |
async { | |
while true do | |
let! msg = inbox.Receive() | |
slowWriter msg | |
}) | |
[1..5] | |
|> List.map (fun i -> makeTask agent.Post i) | |
|> Async.Parallel | |
|> Async.RunSynchronously | |
|> ignore | |
Console.ReadKey() |> ignore |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment