Код IT
← Каталог

Справочник по F# — 6. Параллелизм и конкурентность

Фрагмент из «Справочник по F#»: 6. Параллелизм и конкурентность.

F# main.fs
type Message = 
    | Add of int
    | Get of AsyncReplyChannel<int>

let counterAgent = MailboxProcessor.Start(fun inbox ->
    let rec loop total =
        async {
            let! msg = inbox.Receive()
            match msg with
            | Add n -> return! loop (total + n)
            | Get reply -> 
                reply.Reply(total)
                return! loop total
        }
    loop 0)

// Использование
counterAgent.Post(Add 5)
let current = counterAgent.PostAndReply(fun reply -> Get reply)
type Message = 
    | Add of int
    | Get of AsyncReplyChannel<int>

let counterAgent = MailboxProcessor.Start(fun inbox ->
    let rec loop total =
        async {
            let! msg = inbox.Receive()
            match msg with
            | Add n -> return! loop (total + n)
            | Get reply -> 
                reply.Reply(total)
                return! loop total
        }
    loop 0)

// Использование
counterAgent.Post(Add 5)
let current = counterAgent.PostAndReply(fun reply -> Get reply)