1

Is there an F# equivalent to the TPL Parallel.Invoke? So far, all i have come across is task factory for explicit task control but i am having problems profiling it.

Stuart Gordon
  • 185
  • 2
  • 10

1 Answers1

5

What's wrong with using Parallel.Invoke from F#? One of the best features of F# is that you can re-use all of the functionality available in the .NET Base Class Libraries (BCL).

To simplify things, I'd probably create a little function to map an array of F# functions into an array of Action delegates, like this:

/// Wraps each F# in a System.Action delegate.
let funcsAsActions (funcs : (unit -> unit)[]) : System.Action[] =
    funcs |> Array.map (fun f -> System.Action f)

Then, you can just pass the array returned by funcsAsActions to Parallel.Invoke.

Jack P.
  • 11,487
  • 1
  • 29
  • 34
  • 3
    What's wrong with `funcs |> Array.Parallel.iter (fun f -> f ())`? Mapping to `Action`s first strikes me as pointless. – ildjarn Nov 20 '13 at 16:11
  • @ildjarn Nothing's wrong with that solution either. OP just asked about using `Parallel.Invoke`. There's also an [overload](http://msdn.microsoft.com/en-us/library/dd783942%28v=vs.110%29.aspx) of `Parallel.Invoke` which accepts a `ParallelOptions` so you can tune the execution behavior (which you can't do with `Array.Parallel.iter`). – Jack P. Nov 20 '13 at 18:25
  • 1
    @ildjarn You really should post your comment as an answer – Panagiotis Kanavos Nov 22 '13 at 09:10