gpt4 book ai didi

c# - Go 例程与任务并行库的实现

转载 作者:IT王子 更新时间:2023-10-29 01:53:53 24 4
gpt4 key购买 nike

我刚开始学习围棋。 Go 的优势在于用于处理多个并发连接的 goroutines。提到了

Goroutines can be considered as light-weight threads (but not actually threads) which can grow/shrink stack size and these are multiplexed into multiple os threads. Say if you have 1000 goroutines then these are scheduled to native OS threads based on blocking and waiting modes of goroutines.

基本上,我来自 C# 和 Nodejs 背景。我很困惑它与用 C# 实现的 TaskParallelLibrary 有何不同。

TaskParallelLibrary hides the complexity of creating threads and managing them. You just start a task and CLR takes care of mapping them to native threads. Here you can create thousands of tiny tasks which are mapped and scheduled to OS threads. However TPL solves async problems specifically.

我的问题是 TPL 与 goroutines 有何不同? goroutines 是否使用协程(可暂停函数或?)。 TPL 还将异步/系统调用操作多路复用到线程池,甚至 Go 也将系统调用多路复用到线程池。

如果我的任何假设是错误的,请纠正我。任何人都可以帮助我具体实现的不同之处吗?为什么 goroutines 声称比 TPL 更快?

最佳答案

主要区别在于 Go 运行时将 goroutine 的调度与 I/O 紧密耦合,这基本上是这样工作的:如果 goroutine 将要阻塞某些 I/O 操作或 channel 操作,调度程序会暂停该 goroutine一旦它知道原来的 I/O 或 channel 操作现在可以继续,就重新激活它。这允许以纯粹顺序的方式编写 Go 代码——没有所有回调 hell 和“futures”/“promises”kludges,它们只是将回调包装到对象中,也没有 async/await 机制,同样,它只是将编译器技巧与普通操作系统线程相结合。

这东西在 this classic piece 中解释得很好由 Dart 编程语言的开发人员之一编写。

另见 thisthis .

关于c# - Go 例程与任务并行库的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51962682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com