Go 例程与任务并行库的实现
Implementation of Go-routines vs Task Parallel Library
我刚开始学围棋。 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 还将 async/syscalls 操作多路复用到线程池,甚至 Go 也将系统调用多路复用到线程池。
如果我的任何假设是错误的,请纠正我。任何人都可以帮助我具体实施的不同之处吗?为什么 goroutines 声称比 TPL 更快?
主要区别在于 Go 运行时将 goroutine 的调度与 I/O 紧密耦合,这基本上是这样工作的:如果 goroutine 即将阻塞某些 I/O 操作或通道操作,调度程序暂停该 goroutine 并在知道原始 I/O 或通道操作现在可以继续后重新激活它。这允许以纯顺序的方式编写 Go 代码——没有所有的回调地狱和 "futures"/"promises" 混乱,它们只是将回调包装到对象中,也没有 async
/await
同样,它只是将编译器技巧与普通 OS 线程结合在一起的机器。
Dart 编程语言的一位开发人员在 this classic piece 中对这些内容进行了很好的解释。
我刚开始学围棋。 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 还将 async/syscalls 操作多路复用到线程池,甚至 Go 也将系统调用多路复用到线程池。
如果我的任何假设是错误的,请纠正我。任何人都可以帮助我具体实施的不同之处吗?为什么 goroutines 声称比 TPL 更快?
主要区别在于 Go 运行时将 goroutine 的调度与 I/O 紧密耦合,这基本上是这样工作的:如果 goroutine 即将阻塞某些 I/O 操作或通道操作,调度程序暂停该 goroutine 并在知道原始 I/O 或通道操作现在可以继续后重新激活它。这允许以纯顺序的方式编写 Go 代码——没有所有的回调地狱和 "futures"/"promises" 混乱,它们只是将回调包装到对象中,也没有 async
/await
同样,它只是将编译器技巧与普通 OS 线程结合在一起的机器。
Dart 编程语言的一位开发人员在 this classic piece 中对这些内容进行了很好的解释。