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

标签 c# multithreading go 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 还将异步/系统调用操作多路复用到线程池,甚至 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/

相关文章:

c# - 在流程布局面板中按下了哪个按钮?

.net - 从不是在其上创建线程的线程访问的控件

go - Go 有文件名限制吗?

unit-testing - 如何模拟第三方包的方法

c# - 将 JSON 数据发布到 Microsoft Graph API Azure Function

c# - Azure API App Swagger 无法从门户获得

c# - 替换包含文件中随机数的字符串

java - 使用Runnable进行并行执行

c++ - C++中OpenMP编程嵌套循环的锁策略

datetime - Golang - 结构 : time. 时间