我无法找到如何声明runtime.LockOSThread()和runtime.UnlockOSThread()。我将它定义为[runtime.LockOSThread()]一些代码[runtime.UnlockOSThread()]。但是它给出了错误,未定义:runtime.LockOSThread和undefined:runtime.UnlockOSThread。任何人都可以建议我如何定义它?更多,我将它定义为,
Routine 1 {
runtime.LockOSThread()
do something
runtime.UnlockOSThread
}
main {
routine1
}
答案 0 :(得分:2)
例如,
package main
import "runtime"
func routine() {
runtime.LockOSThread()
runtime.UnlockOSThread()
}
func main() {
go routine()
}
答案 1 :(得分:1)
如有疑问,请参阅the documentation。
func LockOSThread()
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.
func UnlockOSThread()
UnlockOSThread unwires the calling goroutine from its fixed operating system thread.
If the calling goroutine has not called LockOSThread, UnlockOSThread is a no-op.