2011-12-04 32 views
0

我無法找到如何聲明runtime.LockOSThread()runtime.UnlockOSThread()。我將它定義爲[runtime.LockOSThread()]一些代碼[runtime.UnlockOSThread()]。但它給錯誤,未定義:runtime.LockOSThread和undefined:runtime.UnlockOSThread。任何人都可以建議我如何定義它?欲瞭解更多,我將它定義爲,如何聲明runtime.LockOSThread()和runtime.UnlockOSThread()

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

} 

main { 
routine1 
} 

回答

2

例如,

package main 

import "runtime" 

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

func main() { 
    go routine() 
} 
+0

謝謝peterSO。我很抱歉,因爲我正在嘗試執行http://play.golang.org/上的此代碼。這就是它給錯誤的原因。在我的桌面上,此代碼(以及我的示例代碼)也正常運行。 – Arpssss

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. 
+0

問題鏈接爲文檔'runtime.LockOSThread()'和'runtime.UnlockOSThread()'。 – peterSO