<Query Kind="FSharpProgram" />

open System
open System.Threading
open System.Threading.Tasks

let tid () = Thread.CurrentThread.ManagedThreadId;

let a = new MailboxProcessor<int> (fun inbox -> async { () })
    
let b = new MailboxProcessor<int> (fun inbox -> async { () })
    
let acount = ref 4
let adone = new TaskCompletionSource<unit> ()

let c = new MailboxProcessor<int> (fun inbox ->
    let rec loop () = async {
        let! x = a.Receive ()
        let! y = b.Receive ()
        printfn "%d %d" x y
        
        if Interlocked.Decrement acount = 0 then adone.SetResult () 
        else return! loop ()
        }
    loop ())
 
//a.Start ()
//b.Start ()
c.Start ()

Thread.Sleep 1000
a.Post 10
b.Post 20

a.Post 11
a.Post 12
a.Post 13
b.Post 21
b.Post 22
b.Post 23

adone.Task.Wait ()