runtime.LockOSThread是否允许子例程在同一个OS线程中运行? [英] Does runtime.LockOSThread allow child goroutines to run in same OS thread?

查看:164
本文介绍了runtime.LockOSThread是否允许子例程在同一个OS线程中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在Go中, runtime.LockOSThread() 会将一个goroutine绑定到一个OS线程,并且不允许其他goroutine在该线程中执行。
这是否也适用于儿童套房?



例如:

  runtime.LockOSThread()
去func(){
去func(){
//做某事
}()
//做某事
}()

这两个goroutine是否在单个和独占的OS线程中执行或只有第一个?

解决方案

runtime.LockOSThread 的文档说:


LockOSThread将调用goroutine 连接到它当前的操作系统线程。在调用goroutine退出或调用UnlockOSThread之前,它将始终在该线程中执行,并且没有其他goroutine可以



(重点是我的)



这意味着如果Go的某个实现执行了你所要求的操作,那将是错误的。

澄清:如果一个goroutine保留了一个线程,并在同一个线程上执行另一个goroutine;那就是错的。


I understand that in Go, runtime.LockOSThread() will bind a goroutine to one OS thread and not allow other goroutines to execute in that thread. Is this also true for child goroutines?

For example:

runtime.LockOSThread()
go func() {
    go func() {
        // Do something
    }()
    // Do something
}()

Do both of these goroutines execute in a single and exclusive OS thread or only the first one?

解决方案

The documentation for runtime.LockOSThread says:

LockOSThread wires the calling goroutine to its current operating system thread. Until the calling goroutine exits or calls UnlockOSThread, it will always execute in that thread, and no other goroutine can.

(emphasis mine)

This means that if a certain implementation of Go did what you're asking, it would be faulty.

To clarify: if a goroutine had reserved a thread and another goroutine executed on that same thread; that's what would be wrong.

这篇关于runtime.LockOSThread是否允许子例程在同一个OS线程中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆