Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is asynchronous in C# the same implementation as in F#?

Tags:

Is the asynchronous implementation in C# 4.5 exactly the same as in F# 2 in the way threads are used?

like image 815
Andries Avatar asked Oct 03 '12 12:10

Andries


People also ask

Does C have asynchronous?

of course it is.

What is asynchronous in C?

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.

Is C# synchronous or asynchronous?

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).

Is C sharp synchronous?

C# supports both synchronous and asynchronous methods.


1 Answers

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?

like image 158
Tomas Petricek Avatar answered Sep 17 '22 23:09

Tomas Petricek