Skip to content

Instantly share code, notes, and snippets.

@JeffreyZhao
Created July 15, 2009 11:24
Show Gist options
  • Save JeffreyZhao/147655 to your computer and use it in GitHub Desktop.
Save JeffreyZhao/147655 to your computer and use it in GitHub Desktop.
Selective Receive with F# MailboxProcessor
#light
open Microsoft.FSharp.Control
let counter =
MailboxProcessor.Start(fun inbox ->
let rec loop() =
async {
let! msg = inbox.Scan(fun x ->
match x with
| 1 -> Some(async { return x })
| _ -> None)
do printf "%d " msg
let! msg = inbox.Scan(fun x ->
match x with
| 2 -> Some(async { return x })
| _ -> None)
do printf "%d " msg
return! loop() }
loop());
for t = 1 to 5 do counter.Post(1)
for t = 1 to 5 do counter.Post(2)
Console.ReadLine() |> ignore
// output:
// 1 2 1 2 1 2 1 2 1 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment