Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fibers vs async await

I'm joining a C# project in which the developers are heavily using Fibers. Before this project I haven't even heard of them and previously used async await and Threads and BackgroundWorkers to my multitasking operations. Today I was asking them why they used Fibers and the main developer said that it's easier for him to debug. Meaning he knows which thread a particular function has come from and even could access the variables higher in the stack.

I was wondering what are the advantages and disadvantages of using Fibers vs using the new async await and using Threads.

PS: We're using .Net 4.5

like image 903
Alireza Noori Avatar asked Jul 04 '15 13:07

Alireza Noori


People also ask

What is the difference between async await and thread C#?

Threads. Async methods are intended to be non-blocking operations. An await expression in an async method doesn't block the current thread while the awaited task is running. Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method.

Does async await improve performance?

C# Language Async-Await Async/await will only improve performance if it allows the machine to do additional work.

Is async await same as synchronous?

Async/await helps you write synchronous-looking JavaScript code that works asynchronously. Await is in an async function to ensure that all promises that are returned in the function are synchronized. With async/await, there's no use of callbacks.

What is the advantages of async await approach?

The biggest advantage of using async and await is, it is very simple and the asynchronous method looks very similar to a normal synchronous methods. It does not change programming structure like the old models (APM and EAP) and the resultant asynchronous method look similar to synchronous methods.


1 Answers

I was asking them why they used Fibers and the main developer said that it's easier for him to debug. Meaning he knows which thread a particular function has come from and even could access the variables higher in the stack.

That sounds outright peculiar. When using the Task Parallel Library with custom schedulers other than the default ThreadPoolTaskScheduler, you can, yourself, decide how your tasks get scheduled (and it isn't necessarily on new threads). async-await on the other hand provides you a convenient way of doing asynchronous IO. VS gives you the ability to debug asynchronous code using as if it were executing synchronously.

In order to use fibers, one would have to invoke unmanaged API's, as .NET doesn't offer any managed wrappers in the BCL. Even the docs of fibers clearly say there isn't a clear advantage to using them:

In general, fibers do not provide advantages over a well-designed multithreaded application. However, using fibers can make it easier to port applications that were designed to schedule their own threads.


I was wondering what are the advantages and disadvantages of using Fibers vs using the new async await and using Threads.

Using async-await give you the benefit of doing IO bound asynchronous work while feeling like you're executing synchronously. The Task Parallel Library provides an easy way of scheduling work on dedicated threads, be them thread-pool threads or new threads, while allowing you to hook into the mechanism which schedule those units of work. I really see no advantage in using fibers today, with all the framework has to offer.

I think you should tell your main-developer to do some reading on multi-threaded and asynchronous IO work using the Task Parallel Library and async-await, respectively. I think it would make life easier for all of you.

like image 187
Yuval Itzchakov Avatar answered Sep 19 '22 14:09

Yuval Itzchakov