In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces how the shared memory of cooperative communication in Go language is realized. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Using goroutine to realize concurrent programming in Go language based on cooperative program, in terms of syntax structure, the cooperative program of Go language is very simple and only needs to be declared by go keyword. The difficulty lies in the uncertainty caused by concurrency, and in order to coordinate the communication between different programs with this uncertainty, there are two common concurrent communication models: shared memory and message passing.
Next, let's first take a look at how to achieve Go protocol communication through shared memory, and reconstruct the code of the previous tutorial through co-program communication to achieve the elegant exit of the application, create a new memory.go, and write the code as follows:
Package main
Import ("fmt", "runtime", "sync")
Var counter int = 0
Func add (a, b int, lock * sync.Mutex) {c: = a + b lock.Lock () counter++ fmt.Printf ("% d:% d +% d =% d\ n", counter, a, b, c) lock.Unlock ()}
Func main () {start: = time.Now () lock: = & sync.Mutex {} for I: = 0; I
< 10; i++ { go add(1, i, lock) } for { lock.Lock() c := counter lock.Unlock() runtime.Gosched() if c >= 10 {break}} end: = time.Now () consume: = end.Sub (start) .Seconds () fmt.Println ("Program execution time (s):", consume)}
In order to accurately judge the exit time of the master program, we need to notify the master program after all the subprograms have been executed. After receiving the signal, the master program exits the program. By means of shared memory, we introduce a global counter counter, which is shared by all the programs. Each time the subprogram is executed, the value of the counter is added to 1. When all the subprograms are executed, the value of the counter should be 10. We judge the value of counter by an endless loop in the main coordination program. Only when it is greater than or equal to 10:00 will we exit the loop and then exit the whole program.
In addition, because the counter variable is shared by all cooperators, in order to prevent the counter value from being contaminated (two coprograms operate counters at the same time), we also introduce a locking mechanism, namely sync.Mutex, which is a mutex provided by the Go standard library. When a goroutine calls its Lock () method to add a lock Other goroutine must wait until the goroutine calls the Unlock () method of the same sync.Mutex before continuing to access the sync.Mutex (passed to the subroutine through the pointer, so the whole application holds the same mutex). In this way, we can ensure that all the code between lock.Lock () and lock.Unlock () is executed serially in a synchronous blocking manner, thus ensuring that the counter is read and updated. At the same time, only one co-program is operating it (ensuring the atomicity of the operation).
Finally, we also count the execution time of the whole program.
When we execute this code, the print result is as follows:
As you can see, the actual execution time is much less than 1 second, so the overall execution efficiency of the program is nearly 10,000 times faster than the implementation of the previous tutorial.
However, as a result, the code has become more complex and more difficult to maintain, which is just a simple addition implementation. It is necessary to write so much code to introduce shared variables and mutexes to ensure the atomicity of the operation. For more complex business code, locking and unlocking everywhere is obviously a nightmare for developers and maintainers. Since Go takes concurrent programming as the core advantage of the language, Of course, such a problem will not be solved in such a tedious way.
As we said earlier, in addition to shared memory, we can also communicate with each other through message passing. The programming philosophy of Go language is also "Don't communicate by sharing memory, share memory by communicating". So in fact, in the practice of concurrent programming in Go language, we use message passing to realize the communication between co-programs.
In the message passing mechanism, each protocol is an independent individual and has its own variables. unlike shared memory, these variables are not shared among different programs, and there is only one way for the input and output of each program, that is, messages, which is a bit similar to processes: each process is independent and will not be disturbed by other processes, and different processes communicate by messages, and they will not share memory.
On the Go language in the shared memory of the implementation of co-program communication is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.