V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
YanSeven
V2EX  ›  问与答

goroutine 到底算不算一种 coroutine 的实现?

  •  
  •   YanSeven · 3 小时 14 分钟前 · 192 次点击
    之前看论坛里有主题在讨论 goroutine 说不是协程。

    那 coroutine 从原始定义上是什么,为了解决什么问题的。goroutine 是为了解决同样的课题吗?
    其他经典编程语言中如何看待和实现 coroutine 的呢。
    5 条回复    2025-10-28 00:09:11 +08:00
    moudy
        1
    moudy  
       3 小时 7 分钟前
    windows 3.x 以及带着 gil 的 python thread 本质就是 coroutine:合作式多任务,谁不用 cpu 就把 cpu 让出来给别人。缺点是有一个卡死不让 cpu 的,别人也没办法抢 cpu 过来。好处是可以省去大量的同步处理,减少程序对数据一致性的管理开销,最大化利用(单核) cpu 时间做真正有用的事情。

    goroutine 是并行运行的,算是轻量化的线程。
    Dorathea
        2
    Dorathea  
       3 小时 1 分钟前
    以下是自己的想法:

    [Coroutine 的定义]( https://en.wikipedia.org/wiki/Coroutine)是协作式多线程
    其中 co 是 cooperative. [Cooperative 的定义]( https://en.wikipedia.org/wiki/Cooperative_multitasking)

    golang 是没有 yield 主动放弃线程的, routine 之间除了等待 IO 和 system call 或者其他特殊情况外, 本质是抢占资源, 协程之间协作更多的是用 channel.

    好像 golang 也没说过自己是 Coroutine, 文档中说的都是 routine. 为什么会和 Coroutine 联想到一起呢?
    YanSeven
        3
    YanSeven  
    OP
       2 小时 52 分钟前
    @Dorathea 因为基本上所有的中文资料都叫”go 协程“,望文生义字面上理解的话,意思就像是 go 的 coroutine 实现。
    Knuth
        4
    Knuth  
       2 小时 33 分钟前 via iPhone
    n*m ,类似于 bthread
    newtype0092
        5
    newtype0092  
       2 小时 13 分钟前
    @YanSeven 本末倒置了吧,难道不是因为人家是照着 coroutine 取名 goroutine 才被翻译成 go 协程的么。。。
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   1062 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 18:23 · PVG 02:23 · LAX 11:23 · JFK 14:23
    ♥ Do have faith in what you're doing.