英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论