dseax40600 2018-02-07 08:43
浏览 274
已采纳

goroutine池是否像其他语言中的线程池一样有意义?

I build a simple goroutine worker pool with a few chan for a stream of events and it works totally fine. Due to the nature of goroutines I started to ask myself what I gain by doing that other then limiting the concurrent workers. The gorutines them self don't have any state they reuse per execution so that there would be value in keeping them around.

So the question is, does it even make sense to start goroutines and reuse them or just always create a fresh one and just limit the number possible to create/run at the same time?

  • 写回答

1条回答 默认 最新

  • dqzg62440 2018-02-07 14:27
    关注

    Since a goroutine is an executing function, it can be thought of as comprising the set of the followng resources:

    • The code it executes;
    • The state the Go runtime scheduler maintains for that goroutine;
    • The state which is private to the goroutine.

    The latter is values a goroutine has allocated locally (on its stack) and on the heap.

    The code is shared; the state in the scheduler has reasonably negligible cost but the state a goroutine keeps may be costly to re-create.

    The latter point may be used as a justification to keep goroutines around in a pool. But on the other hand, most of the time it's simpler to pool the resources group of goroutines carrying out similar tasks re-use — instead of the goroutines themselves.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问