Home  >  Article  >  Backend Development  >  C# tasks async/await and Golang’s goroutine

C# tasks async/await and Golang’s goroutine

PHPz
PHPzforward
2024-02-09 23:40:09747browse

C# 任务 async/await 与 Golang 的 goroutine

#php editor Baicao will introduce to you today the tasks of async/await in C# language and goroutine in Golang language. Both techniques are methods for implementing concurrent programming that provide simpler and more efficient solutions when dealing with concurrent tasks. By using async/await and goroutine, developers can easily write high-performance, high-concurrency programs, thereby improving application response speed and user experience. Let’s take a look at the characteristics and usage of these two technologies!

Question content

I have been learning C# and Golang for a while and trying to compare the way they support asynchronous programming.

My understanding of goroutine is that the go runtime can schedule goroutine to run on different physical/machine threads. If a goroutine is blocked (for example, waiting for I/O synchronously), the go runtime can suspend the goroutine and release the physical thread to run other goroutines.

C#Task Similar to goroutine in that it is also an abstraction on top of physical threads. However, doing blocking I/O within an async Task is considered a bad idea because "the entire thread will be blocked, causing a deadlock" . The C# runtime cannot do something like a goroutine to suspend a blocked async Task and release the physical thread to run other async Tasks?

I've been struggling with this problem for a while and can't find public material that explains it better. Maybe my understanding is incorrect. Can anyone help me?

Solution

C# Task is similar to goroutine in that it is also an abstraction on top of physical threads.

From a very high level, yes, they may be similar.

However, doing blocking I/O in an asynchronous task is considered a bad idea because "the entire thread will be blocked, causing a deadlock".

Blocking does not necessarily lead to deadlock, but blocking will block the calling thread until the task is completed. That's the point.

C# Can't the runtime do something similar to a goroutine to suspend blocked async tasks and free up the physical thread to run other asynchronous tasks?

Async methods just use await to get the behavior you want. Blocking is specifically used to block threads. Conceptually, goroutines are a bit like making every method async and implicitly using await everywhere.

In the more general case, the C# runtime performs goroutine-like operations to handle any blocking: due to backwards compatibility, the C# runtime cannot easily perform this operation. There is also a lot of legacy code that relies on "special threads" (UI thread, COM STA thread, etc.) where a green thread/coroutine approach would be more difficult. Go essentially avoids backward incompatibility issues by creating an entirely new ecosystem.

The above is the detailed content of C# tasks async/await and Golang’s goroutine. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete