Skip to content

Instantly share code, notes, and snippets.

@sergey-tihon
Created May 28, 2013 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergey-tihon/5665600 to your computer and use it in GitHub Desktop.
Save sergey-tihon/5665600 to your computer and use it in GitHub Desktop.
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