Goroutines and contrast thread

Different stacks

  • Thread: Each OS thread has a fixed size block of memory (typically a 2MB) do stack, the stack will be used to store the current call or being suspended (it means when called by other functions) of the internal variables of the function . Fixed-size stack recursive function calls for more complex or deeper is obviously not enough. Modify fixed size can improve space utilization allows you to create more threads, and can allow a deeper recursive call, but the two are not the same time both.
  • goroutine: a goroutine will begin its life cycle with a small stack, generally only need to 2KB. Goroutine a stack, and the same operating system thread, it will save local variables of function calls active or pending, however, and OS thread stack size is not the same as a goroutine is not fixed; the size of the stack will be needed dynamic stretching. The maximum stack goroutine has 1GB, than traditional fixed-size thread stack is much greater, although under normal circumstances, most goroutine do not need such a large stack.

Different scheduling

  • Thread: OS operating system kernel threads are scheduled. Every few milliseconds, a hardware timer interrupt handler, it will call a kernel function called scheduler. This function suspends the currently executing thread and saved in its memory register contents, check the list of threads which thread can be run once and decides to, and recover the thread register information from memory, and then resume the thread of execution site and start threads of execution. Because the operating system kernel thread is scheduled, so another "move" in need of a full context switch from one thread, i.e., a stored memory state to a user thread, another thread is restored to the register, and update schedule 's data structure. This is a few steps slow, because it requires several local poor memory access, and will increase the cpu cycles running.
  • goroutine: Go runtime contains its own scheduler, the scheduler uses a number of techniques, such as m: n scheduling, because it will be more work (scheduling) m in the n-th goroutine operating system threads. Go to work and the kernel scheduler scheduling is similar, but the scheduler is only concerned with a single Go program goroutine. And thread scheduling the operating system is different, Go scheduler is not using a hardware timer but by the Go language "building" their own scheduling. For example, when a goroutine called time.Sleep or call channel is blocked or mutex operation, the scheduler will put it into hibernation and begin another goroutine until the time is right to go wake up first goroutine. Because this method does not require scheduling into the context of the kernel, so rescheduling a goroutine than one thread scheduling costs are much lower.

GOMAXPROCS

Go scheduler uses a variable called GOMAXPROCS to determine how many will be the operating system threads simultaneously execute Go code. The default value is the number of CPU cores on the operation of the machine, so when a machine core 8, a scheduler can schedule up GO code 8 OS thread. (GOMAXPROCS in front of said m: n Scheduling n). In the dormant goroutine or blocked in communication does not require a corresponding thread is scheduled to do. In the I / O or system call or non-call functions when the Go language is the need for a corresponding operating system threads, but GOMAXPROCS do not need these situations into account.

You can use GOMAXPROCS environment variables to control the parameters explicitly, or may be used to modify runtime.GOMAXPROCS function at runtime. In the following we will see a small program GOMAXPROCS effect of this program will print unlimited 0 and 1.

for {
    go fmt.Print(0)
    fmt.Print(1)
}
$ GOMAXPROCS=1 go run hacker-cliché.go
111111111111111111110000000000000000000011111...

$ GOMAXPROCS=2 go run hacker-cliché.go
010101010101010101011001100101011010010100110...

When first run, while a maximum of only one goroutine be executed. Initially only the main goroutine is executed, it will print a lot. After a period of time, GO scheduler will it put to sleep and wake up another goroutine, this time to start printing a lot of 0, and during printing, goroutine is scheduled on operating system threads. When the second performance, we use two operating system threads, so that two goroutine may be performed together at the same frequency of alternating 0 and 1 print. We must emphasize that goroutine scheduling is affected by many factors, and the runtime is constantly evolving evolution, so you actually get a result here may be due to different versions vary with the results of our operations.

Goroutine not have an ID number

In most support multi-threaded operating systems and programming languages, the current thread has a unique identity (id), and this identity information can be easily be obtained in the form of a common value, typically can be integer or a pointer value. In this case we make abstraction of a thread-local storage (thread local storage, multi-threaded programming content other threads do not want to visit) very easy, you only need to thread id as a map key of the problem can be solved each thread its id will be able to derive value, and other threads do not conflict.

goroutine can not be a programmer to get the concept of identity (id) of. This is a deliberate design to do this, because the thread-local storage will always be abused. For example, a web server is to use one of the supported languages ​​tls achieve, and it is very common to find a lot of information will be a function of the HTTP request, which is to store it on their behalf layer (this may be the storage layer tls) Find of. It's like programs that over-reliance on global variables, like, will lead to a non-healthy "behavior from the outside", in this behavior, the behavior may not be a function of its own internal decision variables, but by it runs in the thread of the decision. Therefore, if the identity of the thread itself will change - such as some worker threads and the like - then the behavior of the function becomes mysterious.

Go encourage simpler patterns, this mode of function parameters are explicit. This will not only make the program more readable, and let us freely without worrying about their identity information to influence the behavior of some given function assigned sub-task.

You should now understand all the language features information write a Go program requires. In the next two sections, we will review some previous examples and tools, we write to support larger program: how a project is organized into a series of packages, how to acquire, build, test, performance test, analyze, write documentation and these packages share it.

Guess you like

Origin www.cnblogs.com/daryl-blog/p/11369601.html