Golang支持LockOSThread()将当前的goroutine专门绑定到当前的OS线程,它也可以UnlockOSThread().
是否有任何用例受益于此功能?
Go运行时(调度程序,垃圾收集器等)可以使用多少个线程?例如,如果GOMAXPROCS是10,那么运行时将使用多少个内核线程?
我读的理由改变GOMAXPROCS对runtime.NumCPU()围棋1.5.有一句话声称"单个goroutine程序的性能可以通过提高GOMAXPROCS运行时的并行性来提高,特别是垃圾收集器."
我真正的问题是:如果我在具有CPU配额的Docker容器中运行单goroutine程序,那么为了获得最大性能,我需要的逻辑处理器的最小数量是多少.
我在多个非缓冲通道上使用select时发现了
select {
case <- chana:
case <- chanb:
}
Run Code Online (Sandbox Code Playgroud)
即使两个通道都有数据,但在处理此选择时,如果chana和case chanb处于不平衡的调用.
package main
import (
"fmt"
_ "net/http/pprof"
"sync"
"time"
)
func main() {
chana := make(chan int)
chanb := make(chan int)
go func() {
for i := 0; i < 1000; i++ {
chana <- 100 * i
}
}()
go func() {
for i := 0; i < 1000; i++ {
chanb <- i
}
}()
time.Sleep(time.Microsecond * 300)
acount := 0
bcount := 0
wg := …Run Code Online (Sandbox Code Playgroud) 如果这个问题太愚蠢,请道歉.我是通过够程的细节阅读这里.根据那个页面,它说Goroutines are multiplexed onto a small number of OS threads, rather than a 1:1 mapping,我所能想到的只有我有限的知识是,产生的OS线程数量有限,其中可能使用用户空间线程或协同程序.它是否正确?如果是这样,如果我可以举一个例子,如果一个程序克隆4个OS线程,其中有多个用户空间线程,并且在所有这4个线程中发生了单个阻塞操作以及非阻塞操作,那么操作系统scheduler context-switch所有这些线程,因为用户空间线程对OS线程不透明?
出于好奇,是否有可能实现goroutines的C实现,这有助于理解内部结构?
我正在Go中为SQRL客户端实施EnScrypt.该函数需要运行,直到它使用最少的CPU时间.我的Python代码如下所示:
def enscrypt_time(salt, password, seconds, n=9, r=256):
N = 1 << n
start = time.process_time()
end = start + seconds
data = acc = scrypt.hash(password, salt, N, r, 1, 32)
i = 1
while time.process_time() < end:
data = scrypt.hash(password, data, N, r, 1, 32)
acc = xor_bytes(acc, data)
i += 1
return i, time.process_time() - start, acc
Run Code Online (Sandbox Code Playgroud)
除了process_time函数之外,将其转换为Go非常简单.我不能使用time.Time/ Timer因为那些测量挂钟时间(受到系统上可能运行的其他所有内容的影响).我需要实际使用的CPU时间,理想情况是函数,或者至少需要运行的线程或进程.
什么是Go相当于process_time?
https://docs.python.org/3/library/time.html#time.process_time
我有下一个代码:
帕戈
package main
import (
"runtime";
"time"
)
func main() {
runtime.GOMAXPROCS(4)
ch := make(chan int)
n := 1
for i := 0; i < n; i++ {
go func() {
time.Sleep(60 * time.Second)
ch <- 1
}();
}
for i := 0; i < n; i++ {
<-ch
}
}
Run Code Online (Sandbox Code Playgroud)
我使用 next 来运行它:
$ go build par.go
$ time ./par
Run Code Online (Sandbox Code Playgroud)
然后,确认该进程有多少个线程:
$ ps -ef | grep par
shubunt+ 3670 32131 0 12:35 pts/0 00:00:00 ./par
$ cat /proc/3670/status …Run Code Online (Sandbox Code Playgroud)