goroutines如何工作

goroutines如何工作

本文介绍了goroutines如何工作? (或:goroutines和OS线程关系)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其他goroutine如何在调用系统调用时继续执行? (当使用GOMAXPROCS = 1时)

据我所知,在调用系统调用时,线程放弃控制,直到syscall返回。
如何在没有在每个blocking-on-syscall goroutine上创建系统线程的情况下实现这种并发?

How can other goroutines keep executing whilst invoking a syscall? (when using GOMAXPROCS=1)
As far as I'm aware of, when invoking a syscall the thread gives up control until the syscall returns.How can Go achieve this concurrency without creating a system thread per blocking-on-syscall goroutine?

从:

它们被称为goroutines,因为现有的术语 - 线程,
协程,进程等 - 传达不准确的内涵。 A
goroutine有一个简单的模型:它是一个函数同时执行
与同一地址空间中的其他goroutine。它是轻量级的,
的成本只比堆栈空间的分配多。并且堆栈
开始小,所以他们很便宜,并通过分配(和释放)
堆存储根据需要增长。

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是多路复用到多个操作系统线程,所以如果一个应该
块,如等待I / O,其他人继续运行。他们的
设计隐藏了许多线程创建和
管理的复杂性。

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被阻塞,运行时将启动一个新的操作系统线程来处理其他goroutine,直到阻塞的阻止阻塞。

If a goroutine is blocking, the runtime will start a new OS thread to handle the other goroutines until the blocking one stops blocking.

引用:

这篇关于goroutines如何工作? (或:goroutines和OS线程关系)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 23:23