Monday 15 July 2013

multithreading - Better understanding of F# Hopac library -



multithreading - Better understanding of F# Hopac library -

i have started using hopac alternative async/tpl , love it. understand basic usage, aspects still not clear.

first, compare alt f# lazy, job within alt evaluated on alt.pick?

second, implementation of autoresetevent right , idiomatic hopac?

/// <summary> /// msdn: autoresetevent class represents local wait handle event resets automatically /// when signaled, after releasing single waiting thread. autoresetevent object automatically /// reset non-signaled scheme after single waiting thread has been released. /// if no threads waiting, event object's state remains signaled. /// /// hopac's alternative http://blogs.msdn.com/b/pfxteam/archive/2012/02/11/10266923.aspx /// </summary> type hopacautoresetevent (initialstate : bool) = // wait on take, , set send allow setchannel : ch<unit> = ch() if initialstate start <| ch.send setchannel () new() = hopacautoresetevent(false) fellow member this.wait(timeout:int) : job<bool> = allow timedout : alt<bool> = ((float timeout) |> timespan.frommilliseconds |> timer.global.timeout) >>=? fun () -> job.result false allow signaled = ch.alt.take setchannel >>=? fun () -> job.result true signaled <|> timedout // docs, of import <|>: // given alternatives processed in left-to-right order short-cut evaluation. // in other words, given alternative of form first <|> second, first alternative // first instantiated and, if pickable, committed , sec alternative // not instantiated @ all. fellow member this.set() : job<unit> = // msdn: also, if set called when there no threads waiting , eventwaithandle // signaled, phone call has no effect. // seek take , send covers cases // if there no waiters , state signalled -> steal state , send // if there waiting thread or state not signaled -> there no signal , steal nothing, signal (ch.try.take setchannel) >>. ch.send setchannel ()

third, implementation of manualresetevent right , idiomatic hopac?

/// <summary> /// hopac's alternative http://blogs.msdn.com/b/pfxteam/archive/2012/02/11/10266920.aspx /// </summary> type hopacmanualresetevent (initialstate : bool) = [<volatilefieldattribute>] allow mutable state : bool = initialstate allow setchannel : mchan<bool> = run <| multicast.create () allow lock = lock.now.create() new() = hopacmanualresetevent(false) fellow member this.wait() : job<bool> = allow rec loop () = job { if state homecoming true else let! port = multicast.port setchannel let! res = (multicast.recv port) // waiting here if res homecoming true else return! loop () } loop () // multicast.fsi: **sends** message of ports listening multicast channel. // send must mean same in ch fellow member this.set() : job<unit> = (multicast.multicast setchannel true) // there no waiters |>> (fun _ -> state <- true ) // in case set state >>% () // , homecoming unit |> (lock.duringjob lock) fellow member this.reset() : job<unit> = (multicast.multicast setchannel false) // (redundant?) if there takers, res in loop() false , loop iterate |>> (fun _ -> state <- false ) // in case set state >>% () |> (lock.duringjob lock)

cross-post: https://github.com/vesakarvonen/hopac/issues/26

multithreading f# hopac

No comments:

Post a Comment