当前位置:网站首页>[go] runtime package for concurrent programming and its common methods
[go] runtime package for concurrent programming and its common methods
2022-06-24 16:19:00 【Cabbage that wants to become powerful】
List of articles
One 、runtime package
1. runtime What is the bag for ?
My last article 【Go】 Concurrent programming Mentioned in ,Go Linguistic goroutine By Runtime (runtime) Scheduled and managed . In this article, we will introduce in detail runtime Knowledge of scheduler .
Even though Go The compiler produces locally executable code , This code still runs in Go Of runtime( This part of the code can be found in runtime Found in the package ) among .Go Linguistic runtime similar Java and .NET The virtual machine used by the language , It is responsible for management including memory allocation 、 Garbage collection ( The first 10.8 section )、 Stack processing 、goroutine、channel、 section (slice)、map And reflection (reflection) wait .
2. runtime Introduction to some methods in the package
runtime The scheduler is a very useful thing , About runtime Package several methods :
Gosched(): Let the current thread give up cpu To let other threads run , It does not suspend the current thread , Therefore, the current thread will continue to execute in the future .
NumCPU(): Returns the of the current system CPU Nuclear quantity .
GOMAXPROCS(): Set the maximum number of simultaneous CPU Check the number .
adopt runtime.GOMAXPROCS function , The application can set up... In the runtime system P The largest number . Be careful , If this value is set during operation , Can cause “Stop the World”. therefore , Should be called at the earliest stage of the application , And it's better to run Go Set the environment variables of the operating program before the program GOMAXPROCS, Instead of calling... In a program runtime.GOMAXPROCS function .
No matter what the integer value we pass to the function is , Runtime system P The maximum value will always be 1~256 Between .
go1.8 after , By default, let the program run on multiple cores , You don't have to set it .
go1.8 front , Still need to set , Can make more efficient use of cpu.Goexit(): Quit current goroutine( however defer The statement will execute as usual ).
NumGoroutine: Returns the total number of tasks in progress and queued .
runtime.NumGoroutine Function after being called , Will return the in a specific state in the system Goroutine The number of . The specific state here refers to Grunnable\Gruning\Gsyscall\Gwaition. In these States Groutine That is, it is regarded as active or being scheduled .
Be careful : Where the garbage is collected Groutine If the state of is also within this range , Will also be included in the counter .GOOS: View the target operating system . A lot of times , We will implement different operations according to different platforms , You can use it GOOS To view your operating system .
runtime.GC: Will make the runtime system perform a mandatory garbage collection .
Mandatory garbage collection : No matter what , All need to be garbage collected . Non mandatory garbage collection : Garbage collection will only be carried out under certain conditions ( The runtime , The unit of heap memory newly requested by the system since the last garbage collection ( Also known as unit increment ) The specified value is reached ).GOROOT() : obtain goroot Catalog .
runtime.LockOSThread and runtime.UnlockOSThread function : The former call will make him call Goroutine With the currently running it M Locked together , The latter call will release such a lock .
Two 、runtime.Gosched()
Let go of the current arrangement CPU Time slice to other collaborators . The waiting time slice of the current collaboration process will continue to be executed in the future .
Release time slice , Let other processes execute first , It executes , And then come back to execute this process .
package main
import (
"fmt"
"runtime"
)
func main() {
go func(s string) {
for i := 0; i < 2; i++ {
fmt.Println(s)
}
}("world")
// Main process
for i := 0; i < 2; i++ {
runtime.Gosched() // The main coordination process is released CPU Time slice , At this point, the above coordination process can be executed
fmt.Println("hello") //CPU Continue after the time slice returns
}
}
Output results :
hello
world
hello
world
or :
world
world
hello
hello
The first result explains : Enter the first round of the main coordination process for loop , The main coordinator gives up CPU Time slot , The above collaboration has not been created yet , So no other collaboration can use time slices , Then the main coordination process continues , First print hello. Enter the second round of the main coordination process for loop , The main coordinator gives up CPU Time slot , The above process is printed world, Then the main process gets the time slice and prints it hello, Before the main process ends , The above process is printed world.
The second result explains : Enter the first round of the main coordination process for loop , The main coordinator gives up CPU Time slot , The above collaboration has been created , And printed two world, Then the main coordination process continues , Printed a hello. Enter the second round of the main coordination process for loop , The main coordinator gives up CPU Time slot , There are no coordination processes waiting to be executed , So the main coroutine continues to print a hello, Then the end .
3、 ... and 、runtime.Goexit()
Exit the current collaboration , however defer The statement will execute as usual .
package main
import (
"fmt"
"runtime"
)
func main() {
go func() {
defer fmt.Println("A.defer")
func() {
defer fmt.Println("B.defer")
runtime.Goexit() // End the current collaboration
defer fmt.Println("C.defer")
fmt.Println("B")
}()
fmt.Println("A")
}()
fmt.Println("main")
}
Output results :
main
B.defer
A.defer
or
main
B.defer
or
main
Before our own collaboration ends , Yes, the defined B.defer and A.defer Of , This explanation :
If we use runtime.Goexit() End of Association , Still execute defer sentence .
The first result explains : The main process prints main, A short period of time before the end of the main coordination process , Our cooperation has been carried out as soon as possible defer sentence : Print the B.defer and A.defer.
The second result explains : The main process prints main, A short period of time before the end of the main coordination process , Although our cooperation process is speeding up , But it only prints B.defer, No time to print A.defer.
The third result explains : The main process prints main, This time, although we are in a hurry, we are in a hurry , But it failed to catch up with the implementation defer sentence , It's all over .
To fully illustrate If we use runtime.Goexit() End of Association , Still execute defer sentence , We can delay the end of the main process :
package main
import (
"fmt"
"runtime"
"time"
)
func main() {
go func() {
defer fmt.Println("A.defer")
func() {
defer fmt.Println("B.defer")
runtime.Goexit() // End the current collaboration
defer fmt.Println("C.defer")
fmt.Println("B")
}()
fmt.Println("A")
}()
time.Sleep(time.Second) // Sleep for a while , Don't let the main coordination process end soon
}
Output results :
B.defer
A.defer
Four 、runtime.GOMAXPROCS()
Golang By default, all tasks run in one cpu Nuclear interior , If you want to in goroutine Use multi-core in , have access to runtime.GOMAXPROCS Function modification , When the parameter is less than 1 Use the default value when .
Go The runtime scheduler uses GOMAXPROCS Parameter to specify how many OS Thread to execute at the same time Go Code . The default value is... On the machine CPU The core number . For example, in a 8 On the core machine , The scheduler will put Go The code is scheduled to 8 individual OS On the thread ( GOMAXPROCS yes m:n In scheduling n).
Go In language, you can use runtime.GOMAXPROCS() Function to set what the current program uses when it is concurrent CPU Logical core number .
Go1.5 Before the release , Single core execution is used by default .Go1.5 After the version , Default to use all of CPU Logical core number .
We can do this by assigning tasks to different CPU The core of logic is to realize the parallel effect , Here's an example :
package main
import (
"fmt"
"runtime"
"time"
)
func a() {
for i := 1; i < 10; i++ {
fmt.Println("A:", i)
}
}
func b() {
for i := 1; i < 10; i++ {
fmt.Println("B:", i)
}
}
func main() {
runtime.GOMAXPROCS(1)
go a()
go b()
time.Sleep(time.Second) // Sleep for a while , Don't let the main coordination process end
}
In the above example , Two tasks have only one logical core , It's time to finish one task and then do another . Set the number of logical cores to 2, At this point, the two tasks are executed in parallel , The code is as follows :
package main
import (
"fmt"
"runtime"
"time"
)
func a() {
for i := 1; i < 10; i++ {
fmt.Println("A:", i)
}
}
func b() {
for i := 1; i < 10; i++ {
fmt.Println("B:", i)
}
}
func main() {
runtime.GOMAXPROCS(2)
go a()
go b()
time.Sleep(time.Second)
}
Go The operating system thread in the language and goroutine The relationship between :
- One operating system thread has more than one user state goroutine.
- go Programs can use multiple operating system threads at the same time .
- goroutine and OS Threads are many to many relationships , namely m:n.
5、 ... and 、runtime.NumCPU()、runtime.GOROOT()、runtime.GOOS
package main
import (
"fmt"
"runtime"
)
func main() {
// obtain cpu Nuclear quantity
fmt.Println("cpus:", runtime.NumCPU())
// obtain goroot Catalog :
fmt.Println("goroot:", runtime.GOROOT())
// Get the operating system
fmt.Println("archive:", runtime.GOOS)
}
Output results :
cpus: 4
goroot: D:\Go
archive: windows
Reference link
- runtime package
- This paper introduces several runtime The most basic function in , To learn more about , Please refer to the article :go-runtime
边栏推荐
- 用 Oasis 开发一个跳一跳(一)—— 场景搭建
- Nature publishes significant progress in quantum computing: the first quantum integrated circuit implementation in history
- Transpose convolution learning notes
- Pytorch transpose convolution
- Leetcode notes of Google boss | necessary for school recruitment!
- Istio FAQ: failed to resolve after enabling smart DNS
- Istio FAQ: sidecar stop sequence
- Detailed explanation of transpose convolution in pytorch
- A memory leak caused by timeout scheduling of context and goroutine implementation
- Some experiences of K project: global template highlights
猜你喜欢

CAP:多重注意力机制,有趣的细粒度分类方案 | AAAI 2021

【云原生 | Kubernetes篇】Kubernetes基础入门(三)

SIGGRAPH 2022 | 真实还原手部肌肉,数字人双手这次有了骨骼、肌肉、皮肤

My network relationship with "apifox"

Understanding openstack network

Nifi from introduction to practice (nanny level tutorial) - environment
MySQL Advanced Series: Locks - Locks in InnoDB

I just came back from the Ali software test. I worked for Alibaba P7 in 3+1, with an annual salary of 28*15

存在安全隐患 部分冒险家混动版将召回

Cap: multiple attention mechanism, interesting fine-grained classification scheme | AAAI 2021
随机推荐
打破内存墙的新利器成行业“热搜”!持久内存让打工人也能玩转海量数据+高维模型
Is Guotai Junan Futures safe? How to open a futures account? How to reduce the futures commission?
Istio FAQ: failed to resolve after enabling smart DNS
Some adventurer hybrid versions with potential safety hazards will be recalled
nifi从入门到实战(保姆级教程)——环境篇
Cap: multiple attention mechanism, interesting fine-grained classification scheme | AAAI 2021
微信公众号调试与Natapp环境搭建
Go deep into the implementation principle of go language defer
Istio FAQ: sidecar stop sequence
Goby+AWVS 实现攻击面检测
A memory leak caused by timeout scheduling of context and goroutine implementation
安装ImageMagick7.1库以及php的Imagick扩展
Detailed explanation of transpose convolution in pytorch
Global and Chinese market for commercial barbecue smokers 2022-2028: Research Report on technology, participants, trends, market size and share
How does the effective date of SAP PP ECM affect the work order?
What is a framework?
MySQL timestamp format conversion date format string
Implement Domain Driven Design - use ABP framework - domain logic & application logic
Understanding of deep separable convolution, block convolution, extended convolution, transposed convolution (deconvolution)
D. Solve the maze (thinking +bfs) codeforces round 648 (Div. 2)