如何声明runtime.LockOSThread()和runtime.UnlockOSThread()

huangapple go评论85阅读模式
英文:

How to declare runtime.LockOSThread() and runtime.UnlockOSThread()

问题

我找不到如何声明runtime.LockOSThread()runtime.UnlockOSThread()的方法。我像这样定义它:[runtime.LockOSThread()]一些代码[runtime.UnlockOSThread()]。但是它报错,显示undefined: runtime.LockOSThread和undefined: runtime.UnlockOSThread。有人能告诉我如何定义它吗?此外,我像这样定义它:

例程1 {
runtime.LockOSThread()
做一些事情
runtime.UnlockOSThread

}

主函数 {
例程1
}
英文:

I could not able to find how to declare runtime.LockOSThread() and runtime.UnlockOSThread(). I define it like [runtime.LockOSThread()] some code [runtime.UnlockOSThread()]. But it is giving error,undefined: runtime.LockOSThread and undefined: runtime.UnlockOSThread. Can anybody suggest me how to define it ? For more, I define it like,

Routine 1 {
runtime.LockOSThread()
do something
runtime.UnlockOSThread

}

main {
routine1
}

答案1

得分: 2

例如,

package main

import "runtime"

func routine() {
    runtime.LockOSThread()
    runtime.UnlockOSThread()
}

func main() {
    go routine()
}
英文:

For example,

package main

import "runtime"

func routine() {
	runtime.LockOSThread()
	runtime.UnlockOSThread()
}

func main() {
	go routine()
}

答案2

得分: 1

当有疑问时,请参考文档

func LockOSThread()

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

func UnlockOSThread()

UnlockOSThread将调用的goroutine与其固定的操作系统线程解绑。
如果调用的goroutine没有调用LockOSThread,则UnlockOSThread不执行任何操作。
英文:

When in doubt, refer to 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.

huangapple
  • 本文由 发表于 2011年12月5日 01:04:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/8376968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定