Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is thread safety of goroutine and shared memory in the foundation of Go language

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Go language basis of goroutine and shared memory thread safety is how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, hope you can get something.

Goroutine is a more lightweight thread

More efficient than threads in Java

Go func () {/ /...} ()

Once the code in the main goroutine is finished, the current Go program ends running, regardless of whether or not the other goroutine is already running.

Let the main goroutine wait for other goroutine:for I: = 0; I < 10; iTunes + {go func () {fmt.Println (I)} ()} time.Sleep (time.Millisecond * 1000) / / goroutine probably takes less time than the set wait time, so this will cause extra waiting time. How can goroutine execute the next goroutine immediately after execution?

WaitGroup provided by go language can achieve this function.

Code modification:

Var wg sync.WaitGroupfor I: = 0; I < 10 Go func + {wg.Add (1) / / add a wait go func () {fmt.Println (I) wg.Done () / / tell Xie Cheng that the waiting transaction has been completed} ()} / * so that we don't have to set the waiting time, but the execution output is as follows: 3 7 4 8 8 8 9 10 10 10. Obviously this approach does not guarantee that goroutine gets a unique integer * / how do you ensure that the number that each thread gets is unique?

Code modification:

Var wg sync.WaitGroupfor I: = 0; I < 10 Go func + {wg.Add (1) / / add a wait go func (j int) {/ / fmt.Println (j) wg.Done () / tell Xiecheng that the waiting transaction has been completed (I) / pass argument I to parameter j} / / execution result: 0 21 6 3 4 5 8 7 9

The change in the I value that we pass to jMagol I does not affect the value of j, so the output is unique.

Shared memory thread safe func TestCounter (t * testing.T) {counter: = 0 for I: = 0; I < 5000 Time.Sleep + {go func () {counter++} ()} time.Sleep (1 * time.Second) t.Logf ("counter =% d", counter)} / / execution result: 4760 has a thread safety problem

Like most languages, go also supports locking to keep threads safe:

Func TestCounterWaitGroup (t * testing.T) {var mut sync.Mutex// creates lock object var wg sync.WaitGroup counter: = 0 for I: = 0; I < 5000 Add a wait go func () {defer func () {mut.Unlock () / / release lock} () mut.Lock () for each start-up go func () ) / / Open the lock counter++ wg.Done () / / tell Xiecheng that the waiting transaction has been completed} ()} wg.Wait () / / wait for the protocol t.Logf ("counter =% d" Counter)}

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report