Created
May 28, 2013 19:54
-
-
Save sergey-tihon/5665600 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
type Message = | |
| Sleep of int*int | |
| SyncPoint of AsyncReplyChannel<unit> | |
let agent = | |
MailboxProcessor.Start(fun inbox -> | |
let rec loop() = | |
async { | |
let! msg = inbox.Receive() | |
match msg with | |
| Sleep(i,time) -> | |
printfn "%d - sleeping %d" i time | |
System.Threading.Thread.Sleep(time) | |
| SyncPoint(replyChannel) -> | |
replyChannel.Reply() | |
return! loop()} | |
loop()) | |
//Task generator | |
let rnd = new System.Random(100) | |
for x in 1..20 do | |
agent.Post (Sleep(x,rnd.Next(1000))) | |
//Sync Point | |
let messageAsync = agent.PostAndAsyncReply(fun replyChannel -> SyncPoint(replyChannel)) | |
Async.RunSynchronously(messageAsync) | |
printfn "FINISH" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment