Is the asynchronous implementation in C# 4.5 exactly the same as in F# 2 in the way threads are used?
of course it is.
In computer programming, the async/await pattern is a syntactic feature of many programming languages that allows an asynchronous, non-blocking function to be structured in a way similar to an ordinary synchronous function.
C# has a language-level asynchronous programming model, which allows for easily writing asynchronous code without having to juggle callbacks or conform to a library that supports asynchrony. It follows what is known as the Task-based Asynchronous Pattern (TAP).
C# supports both synchronous and asynchronous methods.
They are different. The main difference is that C# uses standard .NET Task<T>
to represent asynchronous computations while F# uses its own type called Async<T>
.
More specifically, the key differences are:
A C# async method creates a Task<T>
that is immediately started (hot task model) while F# creates a computation that you have to start explicitly (generator model). This means that F# computations are easier to compose (you can write higher level abstractions).
In F# you also get better control over how is the computation started. You can start a computation using Async.Start
to start it in the background or Async.StartImmediate
to start it on the current thread.
F# asynchronous workflows support cancellation automatically, so you do not have to pass CancellationToken
around.
Perhaps another consequence of the first point is that F# async workflows also support tail-recursion, so you can write recursive workflows (this would not work easily in C#, but C# does not use this programming style)
I wrote a more detailed article about this topic: Asynchronous C# and F# (II.): How do they differ?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With