In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What are the common concurrent programming errors in the go language? most people do not understand the knowledge points of this article, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article, "what are the common concurrent programming errors in the go language?"
Go is a built-in language that supports concurrent programming. Concurrent programming is made easy, flexible, and interesting by using the go keyword to create a cooperative goroutine (lightweight thread) and using channels and other concurrent synchronization methods provided in Go.
On the other hand, Go does not prevent concurrent programming errors caused by carelessness or inexperience of Go programmers. The following sections of this article show some concurrent programming errors that are common in Go programming to help Go programmers avoid making similar mistakes.
There is no synchronization when synchronization is needed.
The lines of code may not run in the order in which they appear.
There are two errors in the following program.
*, reading b in main protocol and writing b in new protocol may lead to data contention.
Second, the condition b = = true does not guarantee a! = nil in the main protocol. In the new protocol, the compiler and CPU may be optimized by reordering instructions, so the runtime b assignment may occur before the an assignment, and when an is modified in the main protocol, it will keep part an as nil.
Package main import ("time"runtime") func main () {var a [] int / / nil var b bool / / false / / a new goroutine go func () {a = make ([] int, 3) b = true / / write b} () for! b {/ / read b time.Sleep (time.Second) runtime.Gosched ()} a [0], a [1] A [2] = 0,1,2 / / might panic}
The above program may work well on one computer, but an exception may be thrown on another. Or it may have run well for N times, but may have thrown an exception on the (Number1) time.
We will use the channels or synchronization methods provided in the sync standard package to ensure the order in memory. For example,
Package main func main () {var a [] int = nil c: = make (chan struct {}) / / a new goroutine go func () {a = make ([] int, 3) c
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.