第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

C 代碼和 goroutine 調(diào)度

C 代碼和 goroutine 調(diào)度

Go
九州編程 2021-09-13 15:39:26
當(dāng)我從 goroutine 調(diào)用 C 代碼時(shí),它是否會(huì)以任何方式影響其他 goroutine 的調(diào)度?我知道如果我在 Erlang 中調(diào)用 NIF,它會(huì)阻塞其他(Erlang)進(jìn)程,直到函數(shù)返回。在 Golang 中是這樣嗎?C 代碼會(huì)阻塞 goroutines 調(diào)度程序嗎?
查看完整描述

2 回答

?
呼如林

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊

從 Go 代碼調(diào)用 C 函數(shù)不會(huì)阻止其他 goroutine 運(yùn)行。

它確實(shí)對調(diào)度程序有影響。運(yùn)行 C 函數(shù)的 goroutine 不一定要計(jì)入GOMAXPROCS限制。它將開始計(jì)算 GOMAXPROCS,但是如果 C 函數(shù)在 sysmon 后臺 goroutine 運(yùn)行時(shí)阻塞了超過 20us,那么如果有一個(gè)準(zhǔn)備運(yùn)行的 goroutine,調(diào)度程序?qū)⒈辉试S啟動(dòng)另一個(gè) goroutine。這些細(xì)節(jié)取決于特定的 Go 版本,可能會(huì)發(fā)生變化。


查看完整回答
反對 回復(fù) 2021-09-13
?
臨摹微笑

TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個(gè)贊

這是一個(gè)非常好的問題,除了代碼之外,我沒有立即找到任何官方聲明。我很高興對官方文檔有任何提示。


答案是否定的, cgo 調(diào)用不會(huì)阻塞調(diào)度程序。


對于以下內(nèi)容,很高興知道 Go 在內(nèi)部使用G表示 goroutine,M表示機(jī)器(線程),P表示處理器。Goroutines 在機(jī)器上運(yùn)行的處理器上運(yùn)行。


根據(jù)代碼文檔,從 G 調(diào)用 C 函數(shù)的工作方式如下:


// To call into the C function f from Go, the cgo-generated code calls

// runtime.cgocall(_cgo_Cfunc_f, frame), where _cgo_Cfunc_f is a

// gcc-compiled function written by cgo.

//

// runtime.cgocall (below) locks g to m, calls entersyscall

// so as not to block other goroutines or the garbage collector,

// and then calls runtime.asmcgocall(_cgo_Cfunc_f, frame).

//

// runtime.asmcgocall (in asm_$GOARCH.s) switches to the m->g0 stack

// (assumed to be an operating system-allocated stack, so safe to run

// gcc-compiled code on) and calls _cgo_Cfunc_f(frame).

//

// _cgo_Cfunc_f invokes the actual C function f with arguments

// taken from the frame structure, records the results in the frame,

// and returns to runtime.asmcgocall.

//

// After it regains control, runtime.asmcgocall switches back to the

// original g (m->curg)'s stack and returns to runtime.cgocall.

//

// After it regains control, runtime.cgocall calls exitsyscall, which blocks

// until this m can run Go code without violating the $GOMAXPROCS limit,

// and then unlocks g from m.

entersyscall本質(zhì)上是告訴運(yùn)行時(shí)這個(gè) goroutine 現(xiàn)在處于“外部”控制之下,就像我們對內(nèi)核進(jìn)行系統(tǒng)調(diào)用的情況一樣。另一種可能是有用的一點(diǎn)是,鎖定g到m(鎖定CGO調(diào)用夠程到操作系統(tǒng)的線程),運(yùn)行時(shí)分配一個(gè)新的OS線程(理論上超過GOMAXPROCS)。


查看完整回答
反對 回復(fù) 2021-09-13
  • 2 回答
  • 0 關(guān)注
  • 259 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號