简体   繁体   English

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

[英]Implementation of Go-routines vs Task Parallel Library

I have just started learning Go.我刚开始学习围棋。 The strength of Go lies in goroutines for handling multiple concurrent connections. Go 的优势在于用于处理多个并发连接的 goroutine。 It was mentioned有人提到

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. Goroutines 可以被认为是轻量级线程(但实际上不是线程),它可以增加/缩小堆栈大小,并且这些线程被复用到多个操作系统线程中。 Say if you have 1000 goroutines then these are scheduled to native OS threads based on blocking and waiting modes of goroutines.假设您有 1000 个 goroutine,那么它们将根据 goroutine 的阻塞和等待模式调度到本机操作系统线程。

Basically, I am from C# and Nodejs background.基本上,我来自 C# 和 Nodejs 背景。 I am pretty confused how it is different from TaskParallelLibrary implemented in C#.我很困惑它与在 C# 中实现的 TaskParallelLibrary 有何不同。

TaskParallelLibrary hides the complexity of creating threads and managing them. TaskParallelLibrary 隐藏了创建和管理线程的复杂性。 You just start a task and CLR takes care of mapping them to native threads.您只需启动一个任务,CLR 就会负责将它们映射到本机线程。 Here you can create thousands of tiny tasks which are mapped and scheduled to OS threads.在这里,您可以创建数千个映射并调度到操作系统线程的小任务。 However TPL solves async problems specifically.然而,TPL 专门解决了异步问题。

My question is how TPL is different from goroutines?我的问题是 TPL 与 goroutines 有何不同? Do goroutines use coroutines (pausable functions or?). goroutine 是否使用协程(可暂停的函数或?)。 TPL also multiplexes the async/syscalls operations to the thread pool, even Go also multiplexes syscalls to the thread pool. TPL 还将 async/syscalls 操作复用到线程池,甚至 Go 也将 syscalls 复用到线程池。

Correct me if any of my assumptions are wrong.如果我的任何假设有误,请纠正我。 Could anyone help me where exactly the implementation differs?任何人都可以帮助我实现的具体不同之处吗? Why do goroutines claim to be faster than TPL?为什么 goroutines 声称比 TPL 更快?

The chief difference is that Go runtime tightly couples scheduling of the goroutines with I/O which basically works like this: if a goroutine is about to block on some I/O operation or on a channel operation, the scheduler suspends that goroutine and re-activates it once it knows the original I/O or channel operation can now proceed.主要区别在于 Go 运行时将 goroutine 的调度与 I/O 紧密耦合,它的工作原理基本上是这样的:如果一个 goroutine 将在某些 I/O 操作或通道操作上阻塞,调度程序会挂起该 goroutine 并重新一旦它知道原始 I/O 或通道操作现在可以继续进行,就会激活它。 This allows writing Go code in purely sequential manner—without all the callback hell and "futures"/"promises" kludges, which merely wraps callbacks into objects, and also without async / await machinery which, again, merely couples compiler tricks with plain OS threads.这允许以纯粹的顺序方式编写 Go 代码——没有所有的回调地狱和“未来”/“承诺”kludges,它只是将回调包装到对象中,也没有async / await机制,这同样只是将编译器技巧与普通操作系统结合起来线程。

This stuff is very good explained in this classic piece by one of the developers of the Dart programming language. Dart 编程语言的一位开发人员在这篇经典文章中很好地解释了这些东西。

Also see this and this .另请参阅

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM