gpt4 book ai didi

concurrency - goroutines 是如何工作的? (或 : goroutines and OS threads relation)

转载 作者:IT老高 更新时间:2023-10-28 12:58:33 26 4
gpt4 key购买 nike

其他 goroutine 如何在调用系统调用时继续执行? (使用 GOMAXPROCS=1 时)
据我所知,在调用系统调用时,线程放弃控制权,直到系统调用返回。Go 如何在不为每个blocking-on-syscall goroutine 创建系统线程的情况下实现这种并发?

来自 documentation :

Goroutines

They're called goroutines because the existing terms—threads, coroutines, processes, and so on—convey inaccurate connotations. A goroutine has a simple model: it is a function executing concurrently with other goroutines in the same address space. It is lightweight, costing little more than the allocation of stack space. And the stacks start small, so they are cheap, and grow by allocating (and freeing) heap storage as required.

Goroutines are multiplexed onto multiple OS threads so if one should block, such as while waiting for I/O, others continue to run. Their design hides many of the complexities of thread creation and management.

最佳答案

如果一个 goroutine 正在阻塞,运行时将启动一个新的 OS 线程来处理其他 goroutine,直到阻塞的 goroutine 停止阻塞。

引用:https://groups.google.com/forum/#!topic/golang-nuts/2IdA34yR8gQ

关于concurrency - goroutines 是如何工作的? (或 : goroutines and OS threads relation),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24599645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com